KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > fields > JahiaDateField


1 // JahiaDateField
2
// YG 08.08.2001
3

4 package org.jahia.data.fields;
5
6 import java.util.Calendar JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Properties JavaDoc;
10 import java.util.TimeZone JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 import org.jahia.data.ConnectionTypes;
14 import org.jahia.exceptions.JahiaException;
15 import org.jahia.params.ParamBean;
16 import org.jahia.registries.ServicesRegistry;
17 import org.jahia.services.fields.ContentDateField;
18 import org.jahia.services.fields.ContentField;
19 import org.jahia.services.pages.JahiaPage;
20 import org.jahia.services.version.EntrySaveRequest;
21 import org.jahia.sharing.FieldSharingManager;
22
23 public class JahiaDateField extends JahiaField implements
24     JahiaAllowApplyChangeToAllLangField {
25     static private TimeZone JavaDoc tz = TimeZone.getTimeZone("UTC");
26     static private Vector JavaDoc months = new Vector JavaDoc();
27     static {
28         months.add("January");
29         months.add("February");
30         months.add("March");
31         months.add("April");
32         months.add("May");
33         months.add("June");
34         months.add("July");
35         months.add("August");
36         months.add("September");
37         months.add("October");
38         months.add("November");
39         months.add("December");
40     }
41
42     /***
43      * constructor
44      * YG
45      *
46      */

47     public JahiaDateField (Integer JavaDoc ID,
48                            Integer JavaDoc jahiaID,
49                            Integer JavaDoc pageID,
50                            Integer JavaDoc ctnid,
51                            Integer JavaDoc fieldDefID,
52                            Integer JavaDoc fieldType,
53                            Integer JavaDoc connectType,
54                            String JavaDoc fieldValue,
55                            Integer JavaDoc rank,
56                            Integer JavaDoc aclID,
57                            Integer JavaDoc versionID,
58                            Integer JavaDoc versionStatus,
59                            String JavaDoc languageCode) {
60         super(ID, jahiaID, pageID, ctnid, fieldDefID, fieldType, connectType,
61               fieldValue, rank, aclID, versionID, versionStatus, languageCode);
62
63         if (isShared()) {
64             this.languageCode = ContentField.SHARED_LANGUAGE;
65         }
66
67     } // end constructor
68

69     public void load (int loadFlag, ParamBean jParams)
70         throws JahiaException {
71         switch (this.getConnectType()) {
72             case (ConnectionTypes.LOCAL):
73
74                 //this.setValue(this.getValue());
75
if (!this.getValue().equals("<empty>")) {
76
77                     int pageID = this.pageID;
78                     JahiaPage thePage = ServicesRegistry.getInstance().
79                                         getJahiaPageService().lookupPage(pageID,
80                         jParams);
81                     String JavaDoc defValue = this.getDefinition().getDefaultValue(
82                         thePage.getPageTemplateID());
83                     //System.out.println("DefValue: "+defValue);
84
String JavaDoc vData = "DD MONTH YYYY";
85                     if ( (defValue.toUpperCase().indexOf("JAHIA_CALENDAR") !=
86                           -1) && (defValue.indexOf("[") != -1)) {
87                         int startStr = defValue.indexOf("[") + 1;
88                         int endStr = defValue.indexOf("]");
89                         vData = defValue.substring(startStr, endStr);
90                     }
91                     //System.out.println("Format: "+vData);
92
long t1;
93                     if (this.getValue() != null && !this.getValue().equals("")) {
94                         try {
95                             Long JavaDoc l1 = new Long JavaDoc(this.getValue());
96                             t1 = l1.longValue();
97                         } catch (NumberFormatException JavaDoc e) {
98                             this.setValue("");
99                             this.setObject(new String JavaDoc(""));
100                             break;
101                         }
102                     } else {
103                         //t1 = System.currentTimeMillis();
104
this.setValue("");
105                         this.setObject(new String JavaDoc(""));
106                         break;
107                     }
108
109                     Calendar JavaDoc cal = Calendar.getInstance(tz);
110                     cal.setTime(new Date JavaDoc(t1));
111                     int year = cal.get(Calendar.YEAR);
112                     int mon = cal.get(Calendar.MONTH);
113                     int day = cal.get(Calendar.DAY_OF_MONTH);
114                     int hour = cal.get(Calendar.HOUR_OF_DAY);
115                     int min = cal.get(Calendar.MINUTE);
116
117                     String JavaDoc vMonth = new String JavaDoc();
118                     vMonth = String.valueOf(1 + mon);
119                     vMonth = (vMonth.length() < 2) ? "0" + vMonth : vMonth;
120                     String JavaDoc vMon = ( (String JavaDoc) months.get(mon)).substring(0, 3);
121                     String JavaDoc vFMon = (String JavaDoc) months.get(mon);
122                     String JavaDoc vY4 = new String JavaDoc();
123                     vY4 = String.valueOf(year);
124                     String JavaDoc vY2 = new String JavaDoc();
125                     vY2 = vY4.substring(2, 4);
126                     String JavaDoc vDD = new String JavaDoc();
127                     vDD = String.valueOf(day);
128                     vDD = (vDD.length() < 2) ? "0" + vDD : vDD;
129                     String JavaDoc h24 = String.valueOf(hour);
130                     String JavaDoc h12;
131                     String JavaDoc vTT;
132                     if (hour > 12) {
133                         h12 = String.valueOf(hour - 12);
134                         vTT = "PM";
135                     } else if (hour == 0) {
136                         h12 = String.valueOf(12);
137                         vTT = "PM";
138                     } else {
139                         h12 = String.valueOf(hour);
140                         vTT = "AM";
141                     }
142                     h24 = (h24.length() < 2) ? "0" + h24 : h24;
143                     h12 = (h12.length() < 2) ? "0" + h12 : h12;
144
145                     String JavaDoc m = String.valueOf(min);
146                     m = (m.length() < 2) ? "0" + m : m;
147                     String JavaDoc vT24 = h24 + ":" + m;
148                     String JavaDoc vT12 = h12 + ":" + m;
149
150                     StringBuffer JavaDoc data = new StringBuffer JavaDoc(vData);
151                     int pos = vData.toUpperCase().indexOf("YYYY");
152                     if (pos != -1) {
153                         data = data.replace(pos, pos + 4, vY4);
154                         vData = data.toString();
155                     }
156                     pos = vData.toUpperCase().indexOf("YY");
157                     if (pos != -1) {
158                         data = data.replace(pos, pos + 2, vY2);
159                         vData = data.toString();
160                     }
161                     pos = vData.toUpperCase().indexOf("MONTH");
162                     if (pos != -1) {
163                         data = data.replace(pos, pos + 5, vFMon);
164                         vData = data.toString();
165                     }
166                     pos = vData.toUpperCase().indexOf("MON");
167                     if (pos != -1) {
168                         data = data.replace(pos, pos + 3, vMon);
169                         vData = data.toString();
170                     }
171                     pos = vData.toUpperCase().indexOf("HH:MM");
172                     if (pos != -1) {
173                         int pos2 = vData.toUpperCase().indexOf("TT");
174                         if (pos2 != -1) {
175                             data = data.replace(pos2, pos2 + 2, vTT);
176                             data = data.replace(pos, pos + 5, vT12);
177                             vData = data.toString();
178                         } else {
179                             data = data.replace(pos, pos + 5, vT24);
180                             vData = data.toString();
181                         }
182
183                     }
184
185                     pos = vData.toUpperCase().indexOf("MM");
186                     if (pos != -1) {
187                         data = data.replace(pos, pos + 2, vMonth);
188                         vData = data.toString();
189                     }
190                     pos = vData.toUpperCase().indexOf("DD");
191                     if (pos != -1) {
192                         data = data.replace(pos, pos + 2, vDD);
193                         vData = data.toString();
194                     }
195
196                     vData = data.toString();
197
198                     //System.out.println("Date: "+vData);
199

200                     this.setObject(this.getValue());
201                     this.setValue(vData);
202                     this.setRawValue(this.getValue());
203                 }
204                 break;
205             case (ConnectionTypes.DATASOURCE):
206                 if ( (loadFlag & LoadFlags.DATASOURCE) != 0) {
207                     this.setValue(FieldSharingManager.getInstance().
208                                   getRemoteFieldValue(
209                         this.getValue()));
210                     this.setObject(this.getValue());
211                 }
212         }
213
214     }
215
216     public boolean save (ParamBean jParams)
217         throws JahiaException {
218         setValue( (String JavaDoc) getObject());
219
220         //System.out.println("########### Save - Value: "+theField.getValue());
221
// 0 for parentAclID in saveField, because field already exists
222
// -> field already has an aclID
223
// -> no need to create a new one
224

225         // deprecated
226
//ServicesRegistry.getInstance().getJahiaFieldService().saveField( theField, 0, jParams );
227

228         EntrySaveRequest saveRequest = new EntrySaveRequest(jParams.getUser(),
229             getLanguageCode());
230         ContentDateField contentField = (ContentDateField) ContentField.
231                                         getField(getID());
232         try {
233             contentField.setValue(getValue(), saveRequest);
234             ServicesRegistry.getInstance().getJahiaSearchService().indexField(
235                 getID(), true, jParams, true);
236         } catch (Throwable JavaDoc t) {
237             t.printStackTrace();
238             return false;
239         }
240         return true;
241     }
242
243     public void delete (ParamBean jParams)
244         throws JahiaException {
245         ;
246     }
247
248     public String JavaDoc getEngineName () {
249         return "org.jahia.engines.shared.Date_Field";
250     }
251
252     public String JavaDoc getFieldContent4Ranking () {
253         return this.getValue();
254     }
255
256     public String JavaDoc getIconNameOff () {
257         return "date_off";
258     }
259
260     public String JavaDoc getIconNameOn () {
261         return "date_on";
262     }
263
264     public JahiaField cloneField (int newctnid, int newPageID, int clonedAclID,
265                                   boolean childrenCloned)
266         throws JahiaException {
267         JahiaField clonedField = ServicesRegistry.getInstance().
268                                  getJahiaFieldService().
269                                  createJahiaField(0, this.getJahiaID(),
270                                                   newPageID, newctnid,
271                                                   this.getFieldDefID(),
272                                                   this.getType(),
273                                                   this.getConnectType(),
274                                                   (String JavaDoc)this.getObject(),
275                                                   this.getRank(),
276                                                   clonedAclID,
277                                                   this.getVersionID(),
278                                                   this.getWorkflowState(),
279                                                   this.getLanguageCode());
280
281         //toDebug("cloneField(): value = "+this.getValue());
282
if (clonedField == null) {
283             throw new JahiaException("Could not clone field.",
284                 "Could not instanciate a new JahiaField object.",
285                 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY);
286         }
287
288         clonedField.setRawValue(this.getRawValue());
289         clonedField.setObject(this.getObject());
290         clonedField.setProperties( (Properties JavaDoc) (this.getProperties()).clone());
291
292         return clonedField;
293     }
294
295     public Object JavaDoc clone () {
296         return new JahiaDateField(new Integer JavaDoc(ID), new Integer JavaDoc(jahiaID),
297                                   new Integer JavaDoc(pageID),
298                                   new Integer JavaDoc(ctnid),
299                                   new Integer JavaDoc(fieldDefID),
300                                   new Integer JavaDoc(fieldType),
301                                   new Integer JavaDoc(connectType),
302                                   fieldValue, new Integer JavaDoc(rank),
303                                   new Integer JavaDoc(aclID),
304                                   new Integer JavaDoc(versionID),
305                                   new Integer JavaDoc(workflowState),
306                                   new String JavaDoc(getLanguageCode()));
307     }
308
309     /**
310      * Is this kind of field shared (i.e. not one version for each language, but one version for every language)
311      */

312     public boolean isShared () {
313         return true;
314     }
315
316     /**
317      * Copy the internal value of current language to another language.
318      * Must be implemented by conctrete field for specific implementation.
319      *
320      * @param aField A same field in another language
321      */

322     public void copyValueInAnotherLanguage (JahiaField aField,
323                                             ParamBean jParams)
324         throws JahiaException {
325         if (aField == null) {
326             return;
327         }
328         aField.setValue(this.getValue());
329         aField.setRawValue(this.getValue());
330         aField.setObject(this.getObject());
331     }
332
333     /**
334      * Returns an Hashmap of language_code/values used by search index engine
335      *
336      * @return
337      */

338     public HashMap JavaDoc getValuesForSearch () throws JahiaException {
339
340         HashMap JavaDoc values = new HashMap JavaDoc();
341
342         String JavaDoc[] vals = new String JavaDoc[1];
343         if ( this.getObject() != null ){
344             vals[0] = (String JavaDoc)this.getObject();
345         } else {
346             vals[0] = "";
347         }
348         values.put(ContentField.SHARED_LANGUAGE,vals);
349         return values;
350     }
351
352 }
353
Popular Tags