KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > containers > ContainerSorterBean


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
//
15
//
16
//
17
// 29.05.2002 NK added in Jahia
18

19
20 package org.jahia.data.containers;
21
22 import java.io.Serializable JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.text.Collator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.BitSet JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Comparator JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Locale JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import org.jahia.bin.Jahia;
38 import org.jahia.data.fields.ExpressionMarker;
39 import org.jahia.exceptions.JahiaException;
40 import org.jahia.params.ParamBean;
41 import org.jahia.registries.ServicesRegistry;
42 import org.jahia.resourcebundle.ResourceBundleMarker;
43 import org.jahia.services.fields.ContentField;
44 import org.jahia.services.version.EntryLoadRequest;
45 import org.jahia.utils.JahiaConsole;
46 import org.jahia.utils.JahiaTools;
47
48 /**
49  * Jahia Standard Containers Sort Handler for a given container list.
50  *
51  * @see ContainerSorterBean
52  * @see JahiaContainerSet
53  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
54  */

55
56 public class ContainerSorterBean implements Serializable JavaDoc, ContainerSorterInterface {
57
58     private static org.apache.log4j.Logger logger =
59         org.apache.log4j.Logger.getLogger(ContainerSorterBean.class);
60
61     private static final String JavaDoc CLASS_NAME = ContainerSorterBean.class.getName();
62
63     protected int ctnListID = -1;
64
65     protected String JavaDoc fieldName;
66
67     protected boolean updated = false;
68
69     protected long lastSortingTime = -1;
70
71     protected boolean numberSort = false;
72
73     protected String JavaDoc numberFormat = NumberFormats.LONG_FORMAT;
74
75     protected boolean isValid = false;
76
77     protected boolean ASC_Ordering = true; // by default ASCENDANT ORDER.
78

79     //** sorted ctnids **/
80
protected Vector JavaDoc result;
81
82     protected EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT;
83
84     //--------------------------------------------------------------------------
85
/**
86      * Constructor
87      *
88      * @param int ctnListID, the container list id.
89      * @param String the field name, the field on which to sort.
90      * @deprecated
91      */

92     public ContainerSorterBean(int ctnListID, String JavaDoc fieldName)
93     throws JahiaException
94     {
95         this(ctnListID,fieldName,null);
96     }
97
98     //--------------------------------------------------------------------------
99
/**
100      * Constructor
101      *
102      * @param int ctnListID, the container list id.
103      * @param String the field name, the field on which to sort.
104      * @param entryLoadRequest
105      * @throws JahiaException
106      */

107     public ContainerSorterBean(int ctnListID, String JavaDoc fieldName,
108                                EntryLoadRequest entryLoadRequest)
109     throws JahiaException
110     {
111         this(ctnListID,fieldName,false,entryLoadRequest);
112     }
113
114     //--------------------------------------------------------------------------
115
/**
116      * Constructor
117      *
118      * @param int ctnListID, the container list id.
119      * @param String the field name, the field on which to sort.
120      * @param boolean , force field values to be converted to long representation before sorting ( if true ).
121      * @param entryLoadRequest
122      * @throws JahiaException
123      */

124     public ContainerSorterBean(int ctnListID, String JavaDoc fieldName, boolean numberSort,
125                                 EntryLoadRequest entryLoadRequest)
126     throws JahiaException
127     {
128         this(ctnListID, fieldName, numberSort, null, entryLoadRequest);
129     }
130
131     //--------------------------------------------------------------------------
132
/**
133      * Constructor
134      *
135      * @param int ctnListID, the container list id.
136      * @param String the field name, the field on which to sort.
137      * @param boolean , force field values to be converted to number representation before sorting ( if true ).
138      * @param numberFormat, only used if numberSort is true. If null, the format used is NumberFormat.LONG_FORMAT
139      * @param entryLoadRequest
140      * @throws JahiaException
141      */

142     public ContainerSorterBean(int ctnListID, String JavaDoc fieldName, boolean numberSort,
143                                String JavaDoc numberFormat, EntryLoadRequest entryLoadRequest)
144     throws JahiaException
145     {
146         if ( ctnListID >0 && fieldName != null && !fieldName.trim().equals("") )
147         {
148             this.ctnListID = ctnListID;
149             this.fieldName = fieldName;
150             this.numberSort = numberSort;
151             if ( NumberFormats.isValidFormat(numberFormat) ){
152                 this.numberFormat = numberFormat;
153             }
154             this.isValid = true;
155         }
156         if (entryLoadRequest != null){
157             this.entryLoadRequest = entryLoadRequest;
158         }
159     }
160
161     //--------------------------------------------------------------------------
162
/**
163      * Constructor
164      *
165      * @param String containerListName, the container list name.
166      * @param ParamBean, the param bean.
167      * @param String the field name, the field on which to sort.
168      * @deprecated
169      */

170     public ContainerSorterBean(String JavaDoc containerListName, ParamBean params, String JavaDoc fieldName)
171     throws JahiaException
172     {
173         this(containerListName,params,fieldName,params.getEntryLoadRequest());
174     }
175
176     //--------------------------------------------------------------------------
177
/**
178      * Constructor
179      *
180      * @param String containerListName, the container list name.
181      * @param ParamBean, the param bean.
182      * @param String the field name, the field on which to sort.
183      * @param entryLoadRequest
184      * @throws JahiaException
185      */

186     public ContainerSorterBean(String JavaDoc containerListName, ParamBean params, String JavaDoc fieldName,
187                                EntryLoadRequest entryLoadRequest)
188     throws JahiaException
189     {
190         this(containerListName,params,fieldName,false,entryLoadRequest);
191     }
192
193     //--------------------------------------------------------------------------
194
/**
195      * Constructor
196      *
197      * @param String containerListName, the container list name.
198      * @param ParamBean, the param bean.
199      * @param String the field name, the field on which to sort.
200      * @param boolean , force field values to be converted to long representation before sorting ( if true ).
201      * @deprecated
202      */

203     public ContainerSorterBean(String JavaDoc containerListName, ParamBean params, String JavaDoc fieldName, boolean numberSort)
204     throws JahiaException
205     {
206         this(containerListName,params,fieldName,numberSort,params.getEntryLoadRequest());
207     }
208
209     //--------------------------------------------------------------------------
210
/**
211      * Constructor
212      *
213      * @param String containerListName, the container list name.
214      * @param ParamBean, the param bean.
215      * @param String the field name, the field on which to sort.
216      * @param boolean , force field values to be converted to long representation before sorting ( if true ).
217      * @param entryLoadRequest
218      * @throws JahiaException
219      */

220     public ContainerSorterBean(String JavaDoc containerListName, ParamBean params, String JavaDoc fieldName,
221                                boolean numberSort, EntryLoadRequest entryLoadRequest)
222     throws JahiaException
223     {
224         this(containerListName,params,fieldName,numberSort,null,entryLoadRequest);
225     }
226
227     //--------------------------------------------------------------------------
228
/**
229      * Constructor
230      *
231      * @param String containerListName, the container list name.
232      * @param ParamBean, the param bean.
233      * @param String the field name, the field on which to sort.
234      * @param boolean , force field values to be converted to number representation before sorting ( if true ).
235      * @param numberFormat, only used if numberSort is true. If null, the format used is NumberFormat.LONG_FORMAT
236      * @param entryLoadRequest
237      * @throws JahiaException
238      */

239     public ContainerSorterBean(String JavaDoc containerListName, ParamBean params, String JavaDoc fieldName,
240                                boolean numberSort, String JavaDoc numberFormat,
241                                EntryLoadRequest entryLoadRequest)
242     throws JahiaException
243     {
244         JahiaConsole.println("ContainerSorterBean","Created container sort for clist: " + containerListName + " on the field " + fieldName);
245
246         if ( containerListName != null ){
247             int clistID = ServicesRegistry.getInstance().getJahiaContainersService().
248                getContainerListID( containerListName, params.getPage().getID() );
249             if ( clistID >0 && fieldName != null && !fieldName.trim().equals("") )
250             {
251                 this.ctnListID = clistID;
252                 this.fieldName = fieldName;
253                 this.numberSort = numberSort;
254                 this.isValid = true;
255                 if ( NumberFormats.isValidFormat(numberFormat) ){
256                     this.numberFormat = numberFormat;
257                 }
258                 //JahiaConsole.println("ContainerSorterBean","Constructor sorter is valid ");
259
}
260         }
261         if (entryLoadRequest != null){
262             this.entryLoadRequest = entryLoadRequest;
263         } else if ( params.getEntryLoadRequest() != null ){
264             this.entryLoadRequest = params.getEntryLoadRequest();
265         }
266     }
267
268     //--------------------------------------------------------------------------
269
/**
270      * Do the sort. Optionally, you can provide a BitSet where each bit set correspond the a container id you want in the result.
271      * If you want all containers in the result, give a null BitSet.
272      *
273      * @param BitSet bits
274      */

275     public Vector JavaDoc doSort(BitSet JavaDoc bits)
276     {
277         JahiaConsole.println("ContainerSorterBean.doSort",
278                                     "Started");
279
280         this.result = null;
281
282         try {
283             Vector JavaDoc ctnIds = null;
284
285             if ( this.isValid ){
286                 // get all container ids
287
JahiaConsole.println("ContainerSorterBean.doSort",
288                                   "Sorting : On field : " + getSortingFieldName());
289                 if ( this.numberSort )
290                 {
291                     this.result = doNumberSort(bits);
292                 } else {
293                     this.result = doStringSort(bits);
294                 }
295             }
296         } catch ( Throwable JavaDoc t ){
297             JahiaConsole.println("ContainerSorterBean.doSort",
298                                     "Exception occured :" + t.getMessage());
299             t.printStackTrace();
300         }
301
302         // Set search time
303
this.lastSortingTime = System.currentTimeMillis();
304
305         return this.result;
306     }
307
308
309     //--------------------------------------------------------------------------
310
/**
311      * Return the vector of sorted ctnids.
312      *
313      */

314     public Vector JavaDoc result()
315     {
316         return this.result;
317     }
318
319     //--------------------------------------------------------------------------
320
/**
321      * Return the order , true - > ASC, false -> DESC.
322      *
323      */

324     public boolean isAscOrdering()
325     {
326         return this.ASC_Ordering;
327     }
328
329     //--------------------------------------------------------------------------
330
/**
331      * Return true, if the values are converted to number before sorting.
332      *
333      */

334     public boolean isNumberOrdering()
335     {
336         return this.numberSort;
337     }
338
339     //--------------------------------------------------------------------------
340
/**
341      * Force or not value to be converted to number before doing the sort.
342      *
343      */

344     public boolean setNumberOrdering(boolean val)
345     {
346         return this.numberSort = val;
347     }
348
349     //--------------------------------------------------------------------------
350
/**
351      * Set DESC ordering.
352      *
353      */

354     public void setDescOrdering()
355     {
356         this.ASC_Ordering = false;
357     }
358
359     //--------------------------------------------------------------------------
360
/**
361      * Set ASC ordering.
362      *
363      */

364     public boolean setAscOrdering()
365     {
366         return this.ASC_Ordering = true;
367     }
368
369     //--------------------------------------------------------------------------
370
/**
371      * Set ASC ordering.
372      *
373      */

374     public boolean setAscOrdering(boolean val)
375     {
376         return this.ASC_Ordering = val;
377     }
378
379     //--------------------------------------------------------------------------
380
/**
381      * Return the container list id.
382      *
383      * @return int ctnListID, the container list id.
384      */

385     public int getCtnListID()
386     {
387         return this.ctnListID;
388     }
389
390     public void setCtnListID(int listId){
391         this.ctnListID = listId;
392     }
393
394     //--------------------------------------------------------------------------
395
/**
396      * Return the sorting field.
397      *
398      * @return int ctnListID, the container list id.
399      */

400     public String JavaDoc getSortingFieldName()
401     {
402         return this.fieldName;
403     }
404
405     //--------------------------------------------------------------------------
406
/**
407      * Return the last sorting time.
408      *
409      * @return long , the last sorting time
410      */

411     public long getLastSortingTime()
412     {
413         return this.lastSortingTime;
414     }
415
416     //--------------------------------------------------------------------------
417
/**
418      * Return true if the sorter initialited properly
419      *
420      * @return boolean, the valid state value.
421      */

422     public boolean isValid()
423     {
424         return this.isValid;
425     }
426
427     //--------------------------------------------------------------------------
428
/**
429      * Return the update status. Each time the doSort method is called, this update status is set to true.
430      *
431      * @return boolean, the internal updated status value.
432      */

433     public boolean getUpdateStatus()
434     {
435         return this.updated;
436     }
437
438     //--------------------------------------------------------------------------
439
/**
440      * Set the update status to true.
441      *
442      */

443     public void setUpdateStatus()
444     {
445         this.updated = true;
446     }
447
448     //--------------------------------------------------------------------------
449
public EntryLoadRequest getEntryLoadRequest(){
450         return this.entryLoadRequest;
451     }
452
453     //--------------------------------------------------------------------------
454
/**
455      * You can reset the internal update status by setting it to false
456      *
457      */

458     public void resetUpdateStatus()
459     {
460         this.updated = false;
461     }
462
463     //--------------------------------------------------------------------------
464
/**
465      * Load an hashtable of pair/value (ctnID,fieldValue) for a given ctnlist and a given fieldName.
466      *
467      *
468      * @param int ctnListID, the container list id
469      * @param String fieldName, the fieldName
470      * @return Vector.
471      */

472     protected Vector JavaDoc getFieldValues(int ctnListID, String JavaDoc fieldName, boolean convertValueAsLong, BitSet JavaDoc bits)
473     throws JahiaException
474     {
475
476         ArrayList JavaDoc deletedCtns = ContainerFilterBean.getDeletedContainers(ctnListID);
477         ArrayList JavaDoc stagingFields = getStagingFields(ctnListID);
478
479         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("SELECT DISTINCT ctnid_jahia_fields_data,b.id_jahia_fields_data,b.value_jahia_fields_data,b.workflow_state, b.language_code FROM jahia_ctn_entries a, jahia_fields_data b, jahia_fields_def c WHERE listid_jahia_ctn_entries=");
480         buff.append(ctnListID);
481         buff.append(" AND ( a.id_jahia_ctn_entries = b.ctnid_jahia_fields_data AND b.fielddefid_jahia_fields_data = c.id_jahia_fields_def AND c.name_jahia_fields_def='");
482         buff.append(JahiaTools.quote(fieldName));
483         buff.append("' ) AND (");
484         buff.append(ContainerFilterBean.buildMultilangAndWorlflowQuery(this.entryLoadRequest,true));
485         buff.append(") ORDER BY ");
486         buff.append(ContainerFilterBean.FIELD_ID);
487         buff.append(",");
488         buff.append(ContainerFilterBean.FIELD_WORKFLOW_STATE);
489
490         String JavaDoc query = buff.toString();
491         Connection JavaDoc dbConn = null;
492         Statement JavaDoc stmt = null;
493         ResultSet JavaDoc rs = null;
494
495         Locale JavaDoc locale = this.getEntryLoadRequest().getFirstLocale(true);
496         if ( locale == null ){
497             locale = Locale.ENGLISH;
498         }
499
500         Collator JavaDoc collator = this.getCollator();
501
502         Vector JavaDoc datas = new Vector JavaDoc();
503         HashMap JavaDoc maps = new HashMap JavaDoc();
504         try
505         {
506             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
507             stmt = dbConn.createStatement();
508             rs = stmt.executeQuery( buff.toString() );
509             while (rs.next())
510             {
511                 int ctnID = rs.getInt(1);
512                 int fieldID = rs.getInt(2);
513                 String JavaDoc fieldValue = rs.getString(3);
514                 int workflowState = rs.getInt(4);
515                 String JavaDoc languageCode = rs.getString(5);
516
517                 if ( bits == null || bits.get(ctnID) ){
518                     if ( this.entryLoadRequest.isCurrent()
519                          || !deletedCtns.contains(new Integer JavaDoc(ctnID))){
520                         if ( workflowState > EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
521                             workflowState = EntryLoadRequest.STAGING_WORKFLOW_STATE;
522                         }
523
524                         if (Jahia.getThreadParamBean() != null) {
525                             // expression marker
526
ExpressionMarker exprMarker = ExpressionMarker.parseMarkerValue(fieldValue, Jahia.getThreadParamBean());
527                             if ( exprMarker != null ){
528                                 try {
529                                     String JavaDoc value = exprMarker.getValue();
530                                     if ( value != null && !"".equals(value) ){
531                                         fieldValue = value;
532                                     }
533                                 }
534                                 catch (Throwable JavaDoc t) {
535                                     logger.debug("Problem while evaluating expression " + exprMarker.getExpr(), t);
536                                 }
537                             }
538                         }
539
540                         // resbundle marker
541
ResourceBundleMarker resMarker = ResourceBundleMarker.parseMarkerValue(fieldValue);
542                         if ( resMarker != null ){
543                             try {
544                                 String JavaDoc value = resMarker.getValue(locale);
545                                 if ( value != null && !"".equals(value) ){
546                                     fieldValue = value;
547                                 }
548                             }
549                             catch (Throwable JavaDoc t) {
550                             }
551                         }
552
553                         TempField aField = new TempField(fieldID,ctnID,0,workflowState,languageCode,fieldValue);
554                         String JavaDoc key = fieldID + "_" + workflowState + "_" + languageCode;
555                         maps.put(key,aField);
556                     }
557                 }
558             }
559         }
560         catch (SQLException JavaDoc se)
561         {
562             String JavaDoc errorMsg = "Error in getFieldValues : " + se.getMessage();
563             JahiaConsole.println ("ContainerFilterBean.getFieldValues", errorMsg);
564         } finally {
565
566             closeStatement (stmt);
567         }
568
569         Vector JavaDoc addedIds = new Vector JavaDoc();
570         int size = maps.size();
571         Iterator JavaDoc iterator = maps.values().iterator();
572         while ( iterator.hasNext() ){
573             TempField aField = (TempField)iterator.next();
574             if ( !addedIds.contains(new Integer JavaDoc(aField.id)) ){
575                 String JavaDoc key = aField.id + "_" + aField.workflowState + "_" +
576                     locale.toString();
577                 if (!aField.languageCode.equals(ContentField.SHARED_LANGUAGE)
578                     && maps.containsKey(key) &&
579                     !aField.languageCode.equals(locale.toString())) {
580                     continue;
581                 }
582                 else if (!aField.languageCode.equals(ContentField.SHARED_LANGUAGE)
583                          && !maps.containsKey(key)) {
584                     // this field doesn't exist in the given language, so sort it as "" value
585
aField.value = "";
586                 }
587
588                 Object JavaDoc obj = null;
589                 if (convertValueAsLong) {
590                     try {
591                         obj = new DataBean(aField.ctnID,
592                                            aField.value);
593                     }
594                     catch (Throwable JavaDoc t) {
595                         // we want to display the ctn anyway
596
obj = new DataBean(aField.ctnID, null);
597                     }
598                 }
599                 else {
600                     obj = new StrDataBean(aField.ctnID, aField.value, collator);
601                 }
602                 if (this.entryLoadRequest.isCurrent()) {
603                     datas.add(obj);
604                     addedIds.add(new Integer JavaDoc(aField.id));
605                 }
606                 else if (this.entryLoadRequest.isStaging()
607                          &&
608                          aField.workflowState >
609                          EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
610                     datas.add(obj);
611                     addedIds.add(new Integer JavaDoc(aField.id));
612                 }
613                 else if (aField.workflowState ==
614                          EntryLoadRequest.ACTIVE_WORKFLOW_STATE
615                          && !stagingFields.contains(new Integer JavaDoc(aField.id))) {
616                     datas.add(obj);
617                     addedIds.add(new Integer JavaDoc(aField.id));
618                 }
619             }
620         }
621
622         return datas;
623     }
624
625     //--------------------------------------------------------------------------
626
/**
627      * Containers are sorted after sorting field's data are loaded and converted to
628      * a long representation.
629      *
630      * @param BitSet bits, any bit position sset to true must correspond to a ctn id to include in the result.
631      * if you want all ctn ids in the result, gieve a null BitSet.
632      * @return Vector, vector of sorted ctn ids.
633      */

634     protected Vector JavaDoc doStringSort(BitSet JavaDoc bits) throws JahiaException
635     {
636         Vector JavaDoc results = new Vector JavaDoc();
637
638         Vector JavaDoc datas = this.getFieldValues(this.ctnListID,this.fieldName,this.isNumberOrdering(),bits);
639
640         Collator JavaDoc collator = this.getCollator();
641         
642         // sort the datas
643
if ( datas.size()>1 ){
644             // a dummy dataBean
645
StrDataBean dummyDataBean = new StrDataBean(ASC_Ordering,collator);
646             Collections.sort(datas,dummyDataBean);
647         }
648         // retrieve sorted ids
649
int size = datas.size();
650         StrDataBean dataBean = null;
651         for ( int i=0; i<size ; i++ ){
652             dataBean = (StrDataBean)datas.get(i);
653             results.add(new Integer JavaDoc(dataBean.ctnID));
654         }
655         return results;
656     }
657
658     //--------------------------------------------------------------------------
659
/**
660      * Containers are sorted after sorting field's data are loaded and converted to
661      * a long representation.
662      *
663      * @param BitSet bits, any bit position sset to true must correspond to a ctn id to include in the result.
664      * if you want all ctn ids in the result, gieve a null BitSet.
665      * @return Vector, vector of sorted ctn ids.
666      */

667     protected Vector JavaDoc doNumberSort(BitSet JavaDoc bits) throws JahiaException
668     {
669         Vector JavaDoc results = new Vector JavaDoc();
670
671         Vector JavaDoc datas = this.getFieldValues(this.ctnListID,this.fieldName,this.isNumberOrdering(),bits);
672         // sort the datas
673
if ( datas.size()>1 ){
674             // a dummy dataBean
675
DataBean dummyDataBean = new DataBean(ASC_Ordering);
676             Collections.sort(datas,dummyDataBean);
677         }
678         // retrieve sorted ids
679
int size = datas.size();
680         DataBean dataBean = null;
681         for ( int i=0; i<size ; i++ ){
682             dataBean = (DataBean)datas.get(i);
683             results.add(new Integer JavaDoc(dataBean.ctnID));
684         }
685         return results;
686     }
687
688     //-------------------------------------------------------------------------
689
protected void closeStatement (Statement JavaDoc statement)
690     {
691         // Close the opened statement
692
try {
693             if (statement!=null) {
694                 statement.close();
695             }
696         }
697         catch (SQLException JavaDoc sqlEx) {
698             // just create an exception without raising it, just to notify it
699
// in the logs.
700
JahiaException je = new JahiaException ("Cannot close a statement",
701                     "Cannot close a statement", JahiaException.DATABASE_ERROR,
702                     JahiaException.WARNING_SEVERITY, sqlEx);
703             logger.error("Error:", je);
704         }
705     }
706
707
708     //--------------------------------------------------------------------------
709
protected class DataBean implements Comparator JavaDoc
710     {
711         int ctnID = 0;
712         String JavaDoc value = null;
713         boolean ASC_Ordering = true;
714
715         public DataBean (int ctnID, String JavaDoc value)
716         {
717             this.ctnID = ctnID;
718             this.value = value;
719         }
720
721         public DataBean (boolean ASC_Ordering)
722         {
723             this.ASC_Ordering = ASC_Ordering;
724         }
725
726         public int compare(Object JavaDoc obj1, Object JavaDoc obj2) throws ClassCastException JavaDoc {
727
728             DataBean dataBean1 = (DataBean)obj1;
729             DataBean dataBean2 = (DataBean)obj2;
730             if ( ASC_Ordering ){
731                 return NumberFormats.compareNumber(dataBean1.value,dataBean2.value, numberFormat);
732             } else {
733                 return NumberFormats.compareNumber(dataBean2.value,dataBean1.value, numberFormat);
734             }
735         }
736     }
737
738     //--------------------------------------------------------------------------
739
protected class StrDataBean implements Comparator JavaDoc
740     {
741         int ctnID = 0;
742         String JavaDoc value = "";
743         boolean ASC_Ordering = true;
744         Collator JavaDoc collator = null;
745
746         public StrDataBean (int ctnID, String JavaDoc value, Collator JavaDoc collator)
747         {
748             this.ctnID = ctnID;
749             this.value = value;
750             this.collator = collator;
751
752             if ( this.collator == null ){
753                 this.collator = Collator.getInstance();
754             }
755         }
756
757         public StrDataBean (boolean ASC_Ordering, Collator JavaDoc collator)
758         {
759             this.ASC_Ordering = ASC_Ordering;
760             this.collator = collator;
761             if ( this.collator == null ){
762                 this.collator = Collator.getInstance();
763             }
764         }
765
766         public int compare(Object JavaDoc obj1, Object JavaDoc obj2) throws ClassCastException JavaDoc {
767
768             StrDataBean dataBean1 = (StrDataBean)obj1;
769             StrDataBean dataBean2 = (StrDataBean)obj2;
770             if ( this.ASC_Ordering ){
771                 return collator.compare(dataBean1.value,dataBean2.value);
772             } else {
773                 return collator.compare(dataBean2.value,dataBean1.value);
774             }
775         }
776     }
777
778     protected class TempField{
779         public int id;
780         public int ctnID;
781         public int versionID;
782         public int workflowState;
783         public String JavaDoc languageCode;
784         public String JavaDoc value;
785
786         public TempField(int id, int ctnID, int versionID, int workflowState,
787                          String JavaDoc languageCode, String JavaDoc value){
788             this.id = id;
789             this.ctnID = ctnID;
790             this.versionID = versionID;
791             this.workflowState = workflowState;
792             this.languageCode = languageCode;
793             this.value = value;
794         }
795     }
796
797     //--------------------------------------------------------------------------
798
/**
799      * Returns an array of fields that are in staging.
800      *
801      * @param ctnListID
802      * @return
803      * @throws JahiaException
804      */

805     protected ArrayList JavaDoc getStagingFields(int ctnListID)
806     throws JahiaException
807     {
808
809         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("SELECT DISTINCT id_jahia_fields_data FROM jahia_ctn_entries a, jahia_fields_data b WHERE a.listid_jahia_ctn_entries=");
810         buff.append(ctnListID);
811         buff.append(" AND a.id_jahia_ctn_entries=b.ctnid_jahia_fields_data AND b.workflow_state>1 ");
812         buff.append(" AND ( ");
813         buff.append(ContainerFilterBean.buildMultilangAndWorlflowQuery(this.getEntryLoadRequest()));
814         buff.append(" )");
815
816         Connection JavaDoc dbConn = null;
817         Statement JavaDoc stmt = null;
818         ResultSet JavaDoc rs = null;
819
820         ArrayList JavaDoc datas = new ArrayList JavaDoc();
821
822         try
823         {
824             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
825             stmt = dbConn.createStatement();
826             rs = stmt.executeQuery( buff.toString() );
827
828             while (rs.next()) {
829                 datas.add(new Integer JavaDoc(rs.getInt(1)));
830             }
831         }
832         catch (SQLException JavaDoc se)
833         {
834             String JavaDoc errorMsg = "Error in getStagingFields() : " + se.getMessage();
835             JahiaConsole.println ("ContainerFilterBean.getStagingFields()", errorMsg);
836         } finally {
837
838             closeStatement (stmt);
839         }
840         return datas;
841     }
842
843     /**
844      * Return the collator instantiated with the first locale from the internal EntryLoadRequest.
845      * If the entryLoadRequest is null, the localtor is instantiated with the default locale of the system
846      * @return
847      */

848     private Collator JavaDoc getCollator(){
849         Collator JavaDoc collator = Collator.getInstance();
850         if ( this.getEntryLoadRequest() != null ){
851             Locale JavaDoc locale = null;
852             locale = this.getEntryLoadRequest().getFirstLocale(true);
853             if ( locale != null ){
854                 collator = Collator.getInstance(locale);
855             }
856         }
857         return collator;
858     }
859
860     public String JavaDoc toString() {
861         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
862         if (result != null) {
863             buf.append("Result:");
864             buf.append(result);
865         }
866         return buf.toString();
867     }
868
869 }
870
Popular Tags