11package es .keensoft .alfresco .action .webscript ;
22
33import java .io .IOException ;
4+ import java .util .Collections ;
5+ import java .util .Comparator ;
6+ import java .util .HashMap ;
7+ import java .util .Iterator ;
8+ import java .util .LinkedHashMap ;
9+ import java .util .LinkedList ;
410import java .util .List ;
11+ import java .util .Map ;
512
613import org .alfresco .model .ContentModel ;
714import org .alfresco .repo .site .SiteServiceImpl ;
@@ -33,6 +40,7 @@ public class DataListWebScript extends AbstractWebScript {
3340 private SiteService siteService ;
3441 private TransactionService transactionService ;
3542 private TaggingService taggingService ;
43+ private Boolean dataListOrdered ;
3644
3745 @ Override
3846 public void execute (WebScriptRequest request , WebScriptResponse response ) throws IOException {
@@ -44,6 +52,7 @@ public void execute(WebScriptRequest request, WebScriptResponse response) throws
4452 try {
4553
4654 List <SiteInfo > sites = siteService .listSites (null , DATALIST_PRESET );
55+ Map <String , String > values = new HashMap <String , String >();
4756
4857 for (SiteInfo site : sites ) {
4958
@@ -61,10 +70,16 @@ public void execute(WebScriptRequest request, WebScriptResponse response) throws
6170 for (ChildAssociationRef item : itemsNodes ) {
6271
6372 if (nodeService .getType (item .getChildRef ()).isMatch (DatalistModel .DATALIST_MODEL_ITEM_TYPE )) {
64- JSONObject obj = new JSONObject ();
65- obj .put (JSON_CODE , nodeService .getProperty (item .getChildRef (), DatalistModel .DATALIST_MODEL_CODE_PROPERTY ).toString ());
66- obj .put (JSON_VALUE , nodeService .getProperty (item .getChildRef (), DatalistModel .DATALIST_MODEL_VALUE_PROPERTY ).toString ());
67- objProcess .put (obj );
73+ // Previous behaviour, include values as they were introduced in Alfresco
74+ if (!dataListOrdered ) {
75+ JSONObject obj = new JSONObject ();
76+ obj .put (JSON_CODE , nodeService .getProperty (item .getChildRef (), DatalistModel .DATALIST_MODEL_CODE_PROPERTY ).toString ());
77+ obj .put (JSON_VALUE , nodeService .getProperty (item .getChildRef (), DatalistModel .DATALIST_MODEL_VALUE_PROPERTY ).toString ());
78+ objProcess .put (obj );
79+ } else {
80+ values .put (nodeService .getProperty (item .getChildRef (), DatalistModel .DATALIST_MODEL_CODE_PROPERTY ).toString (),
81+ nodeService .getProperty (item .getChildRef (), DatalistModel .DATALIST_MODEL_VALUE_PROPERTY ).toString ());
82+ }
6883 } else {
6984 // Ignore other datalist types
7085 continue ;
@@ -77,6 +92,17 @@ public void execute(WebScriptRequest request, WebScriptResponse response) throws
7792 }
7893
7994 }
95+
96+ // Ordered
97+ if (dataListOrdered ) {
98+ Map <String , String > sortedValues = sortByComparator (values );
99+ for (Map .Entry <String , String > entry : sortedValues .entrySet ()) {
100+ JSONObject obj = new JSONObject ();
101+ obj .put (JSON_CODE , entry .getKey ());
102+ obj .put (JSON_VALUE , entry .getValue ());
103+ objProcess .put (obj );
104+ }
105+ }
80106
81107 } catch (Exception e ) {
82108 throw new IOException (e );
@@ -87,6 +113,29 @@ public void execute(WebScriptRequest request, WebScriptResponse response) throws
87113 response .getWriter ().write (jsonString );
88114
89115 }
116+
117+ private static Map <String , String > sortByComparator (Map <String , String > unsortMap ) {
118+
119+ // Convert Map to List
120+ List <Map .Entry <String , String >> list =
121+ new LinkedList <Map .Entry <String , String >>(unsortMap .entrySet ());
122+
123+ // Sort list with comparator, to compare the Map values
124+ Collections .sort (list , new Comparator <Map .Entry <String , String >>() {
125+ public int compare (Map .Entry <String , String > o1 ,
126+ Map .Entry <String , String > o2 ) {
127+ return (o1 .getValue ()).compareTo (o2 .getValue ());
128+ }
129+ });
130+
131+ // Convert sorted map back to a Map
132+ Map <String , String > sortedMap = new LinkedHashMap <String , String >();
133+ for (Iterator <Map .Entry <String , String >> it = list .iterator (); it .hasNext ();) {
134+ Map .Entry <String , String > entry = it .next ();
135+ sortedMap .put (entry .getKey (), entry .getValue ());
136+ }
137+ return sortedMap ;
138+ }
90139
91140 public void setNodeService (NodeService nodeService ) {
92141 this .nodeService = nodeService ;
@@ -104,4 +153,8 @@ public void setTaggingService(TaggingService taggingService) {
104153 this .taggingService = taggingService ;
105154 }
106155
156+ public void setDataListOrdered (Boolean dataListOrdered ) {
157+ this .dataListOrdered = dataListOrdered ;
158+ }
159+
107160}
0 commit comments