Changeset 4619


Ignore:
Timestamp:
03/05/14 09:16:20 (10 years ago)
Author:
Twan Goosen
Message:

implemented resource type counting service

Location:
vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/pojo/ResourceType.java

    r4618 r4619  
    2323 */
    2424public enum ResourceType {
    25     VIDEO, AUDIO, TEXT, ANNOTATION, OTHER
     25    VIDEO, AUDIO, IMAGE, TEXT, ANNOTATION, OTHER
    2626   
    2727}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/ResourceTypeCountingServiceImpl.java

    r4618 r4619  
    1515 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1616 */
    17 
    1817package eu.clarin.cmdi.vlo.service.impl;
    1918
     19import com.google.common.collect.HashMultiset;
     20import com.google.common.collect.Multiset;
     21import eu.clarin.cmdi.vlo.CommonUtils;
     22import eu.clarin.cmdi.vlo.FacetConstants;
    2023import eu.clarin.cmdi.vlo.pojo.ResourceType;
    2124import eu.clarin.cmdi.vlo.pojo.ResourceTypeCount;
    2225import eu.clarin.cmdi.vlo.service.ResourceTypeCountingService;
    23 import java.util.Arrays;
     26import java.util.ArrayList;
    2427import java.util.Collection;
     28import java.util.Collections;
     29import java.util.regex.Pattern;
    2530
    2631/**
     32 * Counts resource types in a resource string collection
    2733 *
    2834 * @author twagoo
     
    3036public class ResourceTypeCountingServiceImpl implements ResourceTypeCountingService {
    3137
     38    private final static String SPLIT_PATTERN = Pattern.quote(FacetConstants.FIELD_RESOURCE_SPLIT_CHAR);
     39
    3240    @Override
    3341    public Collection<ResourceTypeCount> countResourceTypes(Collection<String> resources) {
    34         //mock
    35         return Arrays.asList(new ResourceTypeCount(ResourceType.TEXT, 2),
    36                 new ResourceTypeCount(ResourceType.VIDEO, 1),
    37                 new ResourceTypeCount(ResourceType.OTHER, 1)
    38                 );
     42        if (resources == null || resources.isEmpty()) {
     43            return Collections.emptySet();
     44        }
     45
     46        final Multiset<ResourceType> countBag = HashMultiset.<ResourceType>create(ResourceType.values().length);
     47
     48        // loop over resources and count types
     49        for (String resourceString : resources) {
     50            // split resource string to find mime type
     51            final String[] tokens = resourceString.split(SPLIT_PATTERN, 2);
     52            final String mimeType = tokens[0];
     53            // normalise
     54            final String normalizeMimeType = CommonUtils.normalizeMimeType(mimeType);
     55            // map to ResourceType and add to bag (TODO: normalize to ResourceType directly?)
     56            if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_ANNOTATION)) {
     57                countBag.add(ResourceType.ANNOTATION);
     58            } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_AUDIO)) {
     59                countBag.add(ResourceType.AUDIO);
     60            } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_IMAGE)) {
     61                countBag.add(ResourceType.IMAGE);
     62            } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_TEXT)) {
     63                countBag.add(ResourceType.TEXT);
     64            } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_VIDEO)) {
     65                countBag.add(ResourceType.VIDEO);
     66            }
     67        }
     68
     69        // count items in bag for each resource type
     70        final Collection<ResourceTypeCount> counts = new ArrayList<ResourceTypeCount>(countBag.elementSet().size());
     71        for (ResourceType type : ResourceType.values()) {
     72            final int count = countBag.count(type);
     73            // don't add zero counts
     74            if (count > 0) {
     75                counts.add(new ResourceTypeCount(type, count));
     76            }
     77        }
     78        return counts;
    3979    }
    40    
     80
    4181}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/SearchResultItemPanel.java

    r4618 r4619  
    5353        // get model for resources
    5454        final SolrFieldModel<String> resourcesModel = new SolrFieldModel<String>(model, FacetConstants.FIELD_RESOURCE);
     55        // wrap with a count provider
    5556        final ResouceTypeCountDataProvider countProvider = new ResouceTypeCountDataProvider(resourcesModel, countingService);
     57        // view that shows provided counts
     58        // TODO: hide if no resources
    5659        add(new ResourceCountDataView("resourceCount", countProvider));
    5760    }
    5861
     62    /**
     63     * Label that shows the content of a Solr field by its string value (using
     64     * {@link SolrFieldStringModel})
     65     */
    5966    private static class SolrFieldLabel extends Label {
    6067
     
    7178    }
    7279
     80    /**
     81     * Data view for resource type counts coming from a data provider for
     82     * {@link ResourceTypeCount}
     83     */
    7384    private static class ResourceCountDataView extends DataView<ResourceTypeCount> {
    7485
     
    107118    }
    108119
     120    /**
     121     * One-way converter for resource type counts that generates string
     122     * representations like "1 video file", "2 video files" or "3 text
     123     * documents"
     124     */
    109125    private static class ResourceTypeCountConverter implements IConverter<ResourceTypeCount> {
    110126
     
    134150                case VIDEO:
    135151                    return "video file";
     152                case IMAGE:
     153                    return "image";
    136154                case TEXT:
    137155                    return "text document";
     
    152170                case VIDEO:
    153171                    return "video files";
     172                case IMAGE:
     173                    return "images";
    154174                case TEXT:
    155175                    return "text documents";
Note: See TracChangeset for help on using the changeset viewer.