1 4 package org.jahia.data.fields; 5 6 import java.util.Calendar ; 7 import java.util.Date ; 8 import java.util.HashMap ; 9 import java.util.Properties ; 10 import java.util.TimeZone ; 11 import java.util.Vector ; 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 tz = TimeZone.getTimeZone("UTC"); 26 static private Vector months = new Vector (); 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 47 public JahiaDateField (Integer ID, 48 Integer jahiaID, 49 Integer pageID, 50 Integer ctnid, 51 Integer fieldDefID, 52 Integer fieldType, 53 Integer connectType, 54 String fieldValue, 55 Integer rank, 56 Integer aclID, 57 Integer versionID, 58 Integer versionStatus, 59 String 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 } 69 public void load (int loadFlag, ParamBean jParams) 70 throws JahiaException { 71 switch (this.getConnectType()) { 72 case (ConnectionTypes.LOCAL): 73 74 if (!this.getValue().equals("<empty>")) { 76 77 int pageID = this.pageID; 78 JahiaPage thePage = ServicesRegistry.getInstance(). 79 getJahiaPageService().lookupPage(pageID, 80 jParams); 81 String defValue = this.getDefinition().getDefaultValue( 82 thePage.getPageTemplateID()); 83 String 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 long t1; 93 if (this.getValue() != null && !this.getValue().equals("")) { 94 try { 95 Long l1 = new Long (this.getValue()); 96 t1 = l1.longValue(); 97 } catch (NumberFormatException e) { 98 this.setValue(""); 99 this.setObject(new String ("")); 100 break; 101 } 102 } else { 103 this.setValue(""); 105 this.setObject(new String ("")); 106 break; 107 } 108 109 Calendar cal = Calendar.getInstance(tz); 110 cal.setTime(new Date (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 vMonth = new String (); 118 vMonth = String.valueOf(1 + mon); 119 vMonth = (vMonth.length() < 2) ? "0" + vMonth : vMonth; 120 String vMon = ( (String ) months.get(mon)).substring(0, 3); 121 String vFMon = (String ) months.get(mon); 122 String vY4 = new String (); 123 vY4 = String.valueOf(year); 124 String vY2 = new String (); 125 vY2 = vY4.substring(2, 4); 126 String vDD = new String (); 127 vDD = String.valueOf(day); 128 vDD = (vDD.length() < 2) ? "0" + vDD : vDD; 129 String h24 = String.valueOf(hour); 130 String h12; 131 String 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 m = String.valueOf(min); 146 m = (m.length() < 2) ? "0" + m : m; 147 String vT24 = h24 + ":" + m; 148 String vT12 = h12 + ":" + m; 149 150 StringBuffer data = new StringBuffer (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 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 ) getObject()); 219 220 225 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 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 getEngineName () { 249 return "org.jahia.engines.shared.Date_Field"; 250 } 251 252 public String getFieldContent4Ranking () { 253 return this.getValue(); 254 } 255 256 public String getIconNameOff () { 257 return "date_off"; 258 } 259 260 public String 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 )this.getObject(), 275 this.getRank(), 276 clonedAclID, 277 this.getVersionID(), 278 this.getWorkflowState(), 279 this.getLanguageCode()); 280 281 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 ) (this.getProperties()).clone()); 291 292 return clonedField; 293 } 294 295 public Object clone () { 296 return new JahiaDateField(new Integer (ID), new Integer (jahiaID), 297 new Integer (pageID), 298 new Integer (ctnid), 299 new Integer (fieldDefID), 300 new Integer (fieldType), 301 new Integer (connectType), 302 fieldValue, new Integer (rank), 303 new Integer (aclID), 304 new Integer (versionID), 305 new Integer (workflowState), 306 new String (getLanguageCode())); 307 } 308 309 312 public boolean isShared () { 313 return true; 314 } 315 316 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 338 public HashMap getValuesForSearch () throws JahiaException { 339 340 HashMap values = new HashMap (); 341 342 String [] vals = new String [1]; 343 if ( this.getObject() != null ){ 344 vals[0] = (String )this.getObject(); 345 } else { 346 vals[0] = ""; 347 } 348 values.put(ContentField.SHARED_LANGUAGE,vals); 349 return values; 350 } 351 352 } 353 | Popular Tags |