Skip to content

Commit

Permalink
Ordered values by global property
Browse files Browse the repository at this point in the history
  • Loading branch information
angelborroy-ks committed Apr 19, 2016
1 parent 707f099 commit e9f63f7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion datalist-model-repo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>es.keensoft</groupId>
<artifactId>datalist-model-repo</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<name>datalist-model-repo Repository AMP project</name>
<packaging>amp</packaging>
<description>Manages the lifecycle of the site-props-repo Repository AMP (Alfresco Module Package)</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<property name="siteService" ref="SiteService"/>
<property name="transactionService" ref="TransactionService" />
<property name="taggingService" ref="TaggingService" />
<property name="dataListOrdered" value="${datalist.show.ordered}" />
</bean>

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## This Alfresco Repo Configuration file should be used for custom properties that are introduced by this module.
## Define default values for all properties here.
## System Administrators can override these values in environment specific configurations in
## alfresco/tomcat/shared/classes/alfresco-global.properties.
##

# datalist.show.ordered=true
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package es.keensoft.alfresco.action.webscript;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.site.SiteServiceImpl;
Expand Down Expand Up @@ -33,6 +40,7 @@ public class DataListWebScript extends AbstractWebScript {
private SiteService siteService;
private TransactionService transactionService;
private TaggingService taggingService;
private Boolean dataListOrdered;

@Override
public void execute(WebScriptRequest request, WebScriptResponse response) throws IOException {
Expand All @@ -44,6 +52,7 @@ public void execute(WebScriptRequest request, WebScriptResponse response) throws
try {

List<SiteInfo> sites = siteService.listSites(null, DATALIST_PRESET);
Map<String, String> values = new HashMap<String, String>();

for (SiteInfo site : sites) {

Expand All @@ -61,10 +70,16 @@ public void execute(WebScriptRequest request, WebScriptResponse response) throws
for (ChildAssociationRef item : itemsNodes) {

if (nodeService.getType(item.getChildRef()).isMatch(DatalistModel.DATALIST_MODEL_ITEM_TYPE)) {
JSONObject obj = new JSONObject();
obj.put(JSON_CODE, nodeService.getProperty(item.getChildRef(), DatalistModel.DATALIST_MODEL_CODE_PROPERTY).toString());
obj.put(JSON_VALUE, nodeService.getProperty(item.getChildRef(), DatalistModel.DATALIST_MODEL_VALUE_PROPERTY).toString());
objProcess.put(obj);
// Previous behaviour, include values as they were introduced in Alfresco
if (!dataListOrdered) {
JSONObject obj = new JSONObject();
obj.put(JSON_CODE, nodeService.getProperty(item.getChildRef(), DatalistModel.DATALIST_MODEL_CODE_PROPERTY).toString());
obj.put(JSON_VALUE, nodeService.getProperty(item.getChildRef(), DatalistModel.DATALIST_MODEL_VALUE_PROPERTY).toString());
objProcess.put(obj);
} else {
values.put(nodeService.getProperty(item.getChildRef(), DatalistModel.DATALIST_MODEL_CODE_PROPERTY).toString(),
nodeService.getProperty(item.getChildRef(), DatalistModel.DATALIST_MODEL_VALUE_PROPERTY).toString());
}
} else {
// Ignore other datalist types
continue;
Expand All @@ -77,6 +92,17 @@ public void execute(WebScriptRequest request, WebScriptResponse response) throws
}

}

// Ordered
if (dataListOrdered) {
Map<String, String> sortedValues = sortByComparator(values);
for (Map.Entry<String, String> entry : sortedValues.entrySet()) {
JSONObject obj = new JSONObject();
obj.put(JSON_CODE, entry.getKey());
obj.put(JSON_VALUE, entry.getValue());
objProcess.put(obj);
}
}

} catch (Exception e) {
throw new IOException(e);
Expand All @@ -87,6 +113,29 @@ public void execute(WebScriptRequest request, WebScriptResponse response) throws
response.getWriter().write(jsonString);

}

private static Map<String, String> sortByComparator(Map<String, String> unsortMap) {

// Convert Map to List
List<Map.Entry<String, String>> list =
new LinkedList<Map.Entry<String, String>>(unsortMap.entrySet());

// Sort list with comparator, to compare the Map values
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
public int compare(Map.Entry<String, String> o1,
Map.Entry<String, String> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});

// Convert sorted map back to a Map
Map<String, String> sortedMap = new LinkedHashMap<String, String>();
for (Iterator<Map.Entry<String, String>> it = list.iterator(); it.hasNext();) {
Map.Entry<String, String> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}

public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
Expand All @@ -104,4 +153,8 @@ public void setTaggingService(TaggingService taggingService) {
this.taggingService = taggingService;
}

public void setDataListOrdered(Boolean dataListOrdered) {
this.dataListOrdered = dataListOrdered;
}

}

0 comments on commit e9f63f7

Please sign in to comment.