From 00ef38d6e87152835a798ea1607ae1e138252458 Mon Sep 17 00:00:00 2001 From: David Draper Date: Mon, 19 May 2014 12:29:03 +0000 Subject: [PATCH] Merged HEAD-BUG-FIX (4.3/Cloud) to HEAD (4.3/Cloud) 70596: Merged WAT1 (4.3/Cloud) to HEAD-BUG-FIX (4.3/Cloud) 67363: ACE-1264, ACE-1270: added support for the facet value results, to include display-name for userID and mimetype. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@70828 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../repo/jscript/ScriptFacetResult.java | 71 ++++++++++++++ .../org/alfresco/repo/jscript/Search.java | 98 ++++++++++++++++++- 2 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 source/java/org/alfresco/repo/jscript/ScriptFacetResult.java diff --git a/source/java/org/alfresco/repo/jscript/ScriptFacetResult.java b/source/java/org/alfresco/repo/jscript/ScriptFacetResult.java new file mode 100644 index 0000000000..96517fa944 --- /dev/null +++ b/source/java/org/alfresco/repo/jscript/ScriptFacetResult.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2005-2014 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.jscript; + +import java.io.Serializable; + +/** + * Scriptable facet. Specific for use by Search script as part of the object model. + * + * @author Jamal Kaabi-Mofrad + */ +public class ScriptFacetResult implements Serializable +{ + private static final long serialVersionUID = -6948514033689491531L; + + private final String facetValue; + private final String facetLabel; + private final int hits; + + /** + * @param facetValue the facet value. e.g. the content creator's userID + * @param facetLabel the display name of the {@code facetValue}. e.g. jdoe => John Doe + * @param hits the number of hits + */ + public ScriptFacetResult(String facetValue, String facetLabel, int hits) + { + this.facetValue = facetValue; + this.facetLabel = facetLabel; + this.hits = hits; + } + + /** + * @return the facetValue + */ + public String getFacetValue() + { + return this.facetValue; + } + + /** + * @return the facetLabel + */ + public String getFacetLabel() + { + return this.facetLabel; + } + + /** + * @return the hits + */ + public int getHits() + { + return this.hits; + } +} diff --git a/source/java/org/alfresco/repo/jscript/Search.java b/source/java/org/alfresco/repo/jscript/Search.java index fbadf17b82..3f2e10228b 100644 --- a/source/java/org/alfresco/repo/jscript/Search.java +++ b/source/java/org/alfresco/repo/jscript/Search.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Alfresco Software Limited. + * Copyright (C) 2005-2014 Alfresco Software Limited. * * This file is part of Alfresco * @@ -20,6 +20,7 @@ package org.alfresco.repo.jscript; import java.io.Serializable; import java.io.StringReader; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashSet; @@ -32,6 +33,7 @@ import org.alfresco.repo.management.subsystems.SwitchableApplicationContextFacto import org.alfresco.repo.model.Repository; import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.repository.ContentReader; +import org.alfresco.service.cmr.repository.MimetypeService; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.StoreRef; @@ -891,15 +893,22 @@ public class Search extends BaseScopableProcessorExtension meta.put("numberFound", results.getNumberFound()); meta.put("hasMore", results.hasMore()); // results facets - Map> facetMeta = new HashMap<>(); + Map> facetMeta = new HashMap<>(); for (FieldFacet ff: sp.getFieldFacets()) { // for each field facet, get the facet results List> fs = results.getFieldFacet(ff.getField()); - HashMap facets = new HashMap<>(); - for (Pair f: fs) + List facets = new ArrayList<>(); + for (Pair f : fs) { - facets.put(f.getFirst(), f.getSecond()); + // ignore zero hit fields + if (f.getSecond() > 0) + { + String facetValue = f.getFirst(); + Field field = getFieldType(ff.getField()); + String label = (field == null) ? facetValue : field.getLabel(services, facetValue); + facets.add(new ScriptFacetResult(facetValue, label, f.getSecond())); + } } // store facet results per field facetMeta.put(ff.getField(), facets); @@ -952,4 +961,83 @@ public class Search extends BaseScopableProcessorExtension public String column; public boolean asc; } + + /** + * @author Jamal Kaabi-Mofrad + */ + private enum Field + { + CREATOR("creator.__"), MODIFIER("modifier.__"), MIMETYPE("content.mimetype") + { + @Override + /*Package access level*/ + String getLabel(ServiceRegistry services, String facetValue) + { + MimetypeService mimetypeService = services.getMimetypeService(); + Map mimetypes = mimetypeService.getDisplaysByMimetype(); + String displayName = mimetypes.get(facetValue); + return displayName == null ? facetValue : displayName.trim(); + } + }; + + private Field(String facetField) + { + this.facetField = facetField; + } + + private String facetField; + + private String getFacetField() + { + return facetField; + } + + /** + * Default implementation which will return the full user name from + * the facetValue, if the facetValue represent a userID + * + * @param services the ServiceRegistry + * @param facetValue the facet value + * @return the full user name. If the user doesn't exist then, the + * {@code facetValue} will be returned. + */ + /*Package access level*/ + String getLabel(ServiceRegistry services, String facetValue) + { + String name = null; + + final NodeRef personRef = services.getPersonService().getPersonOrNull(facetValue); + if (personRef != null) + { + final NodeService nodeService = services.getNodeService(); + final String firstName = (String) nodeService.getProperty(personRef, ContentModel.PROP_FIRSTNAME); + final String lastName = (String) nodeService.getProperty(personRef, ContentModel.PROP_LASTNAME); + name = (firstName != null ? firstName + " " : "") + (lastName != null ? lastName : ""); + } + return name == null ? facetValue : name.trim(); + } + } + + /** + * Gets the facet field. + * + * @param facetField the facet field value + * @return the Field type + */ + private Field getFieldType(String facetField) + { + if (facetField == null) + { + return null; + } + + for (Field val : Field.values()) + { + if (facetField.endsWith(val.getFacetField())) + { + return val; + } + } + return null; + } }