1 121 package com.ivata.groupware.util; 122 123 import java.io.Serializable ; 124 import java.text.MessageFormat ; 125 import java.text.ParseException ; 126 import java.text.SimpleDateFormat ; 127 import java.util.Date ; 128 import java.util.GregorianCalendar ; 129 import java.util.Locale ; 130 import java.util.TimeZone ; 131 132 import org.apache.log4j.Logger; 133 134 import com.ivata.groupware.admin.security.server.SecuritySession; 135 import com.ivata.groupware.admin.setting.Settings; 136 import com.ivata.groupware.admin.setting.SettingsInitializationException; 137 import com.ivata.mask.util.SystemException; 138 import com.ivata.mask.web.format.DateFormatter; 139 import com.ivata.mask.web.format.DateFormatterConstants; 140 import com.ivata.mask.web.format.DateFormatterException; 141 142 143 155 public class SettingDateFormatter implements Serializable , DateFormatter { 156 159 private static Logger log = Logger.getLogger(SettingDateFormatter.class); 160 165 private int dateFormat = DateFormatterConstants.DATE_LONG; 166 171 String dateFormatString = null; 172 186 private String dateTimeText = "{0}, ''at'' {1}"; 187 190 private SimpleDateFormat format; 191 196 MessageFormat messageFormat; 197 202 String pattern = null; 203 204 210 SecuritySession securitySession; 211 214 private Settings settings; 215 220 private int timeFormat = DateFormatterConstants.TIME_SHORT; 221 226 String timeFormatString = null; 227 231 private TimeZone timeZone; 232 233 244 private String userName = null; 245 246 252 public SettingDateFormatter(SecuritySession securitySession, 253 Settings settings) { 254 this.securitySession = securitySession; 255 this.settings = settings; 256 } 257 258 275 private void createFormat(Date date) throws SettingsInitializationException, DateFormatterException { 276 if (securitySession == null) { 277 String message = "createFormat - securitySession is null"; 278 log.error(message); 279 throw new NullPointerException (message); 280 } 281 if (settings == null) { 283 throw new SettingsInitializationException("ERROR in SettingDateFormatter: null settings: you must first call setSettings."); 284 } 285 try { 291 if (messageFormat == null) { 292 messageFormat = new MessageFormat (dateTimeText); 293 } 294 if (((dateFormat == DateFormatterConstants.DATE_RELATIVE) || 298 (dateFormat == DateFormatterConstants.DATE_SHORT_RELATIVE)) && 299 (date == null)) { 300 format = null; 301 return; 302 } 303 String formatString="", dateFormatSetting="", timeFormatSetting=""; 305 306 switch (dateFormat) { 307 case DateFormatterConstants.DATE_LONG: 308 dateFormatSetting = "i18nDateLong"; 309 break; 310 311 case DateFormatterConstants.DATE_LONG_YEAR: 312 dateFormatSetting = "i18nDateLongYear"; 313 break; 314 315 case DateFormatterConstants.DATE_LONG_DAY: 316 dateFormatSetting = "i18nDateLongDay"; 317 break; 318 319 case DateFormatterConstants.DATE_SHORT: 320 dateFormatSetting = "i18nDateShort"; 321 break; 322 323 case DateFormatterConstants.DATE_SHORT_YEAR: 324 dateFormatSetting = "i18nDateShortYear"; 325 break; 326 327 case DateFormatterConstants.DATE_INPUT: 328 dateFormatSetting = "i18nDateInput"; 329 break; 330 331 case DateFormatterConstants.DATE_INPUT_DISPLAY: 332 dateFormatSetting = "i18nDateInputDisplay"; 333 break; 334 335 case DateFormatterConstants.DATE_SHORT_RELATIVE: 336 case DateFormatterConstants.DATE_RELATIVE: 337 GregorianCalendar now = new GregorianCalendar (); 338 339 now.setTime(new Date ()); 340 GregorianCalendar yesterday = new GregorianCalendar (); 341 342 yesterday.setTime(new Date ()); 343 yesterday.roll(GregorianCalendar.DATE, false); 344 GregorianCalendar lastWeek = new GregorianCalendar (); 345 346 lastWeek.setTime(new Date ()); 347 lastWeek.roll(GregorianCalendar.WEEK_OF_YEAR, false); 348 GregorianCalendar compare = new GregorianCalendar (); 349 350 compare.setTime(date); 351 if ((now.get(GregorianCalendar.YEAR) == compare.get(GregorianCalendar.YEAR)) && 353 (now.get(GregorianCalendar.DAY_OF_YEAR) == compare.get(GregorianCalendar.DAY_OF_YEAR))) { 354 if (dateFormat == DateFormatterConstants.DATE_SHORT_RELATIVE) { 355 dateFormatSetting = "i18nDateShortToday"; 356 } else { 357 int hour = compare.get(GregorianCalendar.HOUR_OF_DAY); 359 360 if (hour < 12) { 361 dateFormatSetting = "i18nDateThisMorning"; 362 } else if (hour < 18) { 363 dateFormatSetting = "i18nDateThisAfternoon"; 364 } else { 365 dateFormatSetting = "i18nDateThisEvening"; 366 } 367 } 368 } else if ((now.get(GregorianCalendar.YEAR) == compare.get(GregorianCalendar.YEAR)) && 369 (yesterday.get(GregorianCalendar.DAY_OF_YEAR) == compare.get(GregorianCalendar.DAY_OF_YEAR))) { 370 if (dateFormat == DateFormatterConstants.DATE_SHORT_RELATIVE) { 371 dateFormatSetting = "i18nDateShortYesterday"; 372 } else { 373 int hour = compare.get(GregorianCalendar.HOUR_OF_DAY); 375 376 if (hour < 12) { 377 dateFormatSetting = "i18nDateYesterdayMorning"; 378 } else if (hour < 18) { 379 dateFormatSetting = "i18nDateYesterdayAfternoon"; 380 } else { 381 dateFormatSetting = "i18nDateYesterdayEvening"; 382 } 383 } 384 } else if ((now.get(GregorianCalendar.YEAR) == compare.get(GregorianCalendar.YEAR)) && 385 (now.get(GregorianCalendar.WEEK_OF_YEAR) == compare.get(GregorianCalendar.WEEK_OF_YEAR))) { 386 dateFormatSetting = "i18nDateWeekDay"; 388 } else if (now.get(GregorianCalendar.YEAR) == compare.get(GregorianCalendar.YEAR)) { 389 if (dateFormat == DateFormatterConstants.DATE_SHORT_RELATIVE) { 391 dateFormatSetting = "i18nDateShort"; 392 } else { 393 dateFormatSetting = "i18nDateLong"; 394 } 395 } else { 396 if (dateFormat == DateFormatterConstants.DATE_SHORT_RELATIVE) { 398 dateFormatSetting = "i18nDateShortYear"; 399 } else { 400 dateFormatSetting = "i18nDateLongYear"; 401 } 402 } 403 break; 404 } 405 dateFormatString = settings.getStringSetting(securitySession, dateFormatSetting, 406 securitySession.getUser()); 407 408 switch (timeFormat) { 410 case DateFormatterConstants.TIME_LONG: 411 timeFormatSetting = "i18nTimeLong"; 412 break; 413 414 case DateFormatterConstants.TIME_SHORT: 415 timeFormatSetting = "i18nTimeShort"; 416 break; 417 418 case DateFormatterConstants.TIME_INPUT: 419 timeFormatSetting = "i18nTimeInput"; 420 break; 421 422 case DateFormatterConstants.TIME_INPUT_DISPLAY: 423 timeFormatSetting = "i18nTimeInputDisplay"; 424 break; 425 426 } 427 timeFormatString = settings.getStringSetting(securitySession, 428 timeFormatSetting, securitySession.getUser()); 429 430 Object arguments[] = { 433 dateFormatString, 434 timeFormatString 435 }; 436 Locale locale = 437 new Locale (settings.getStringSetting(securitySession, 438 "i18nLocaleLanguage", securitySession.getUser()), 439 settings.getStringSetting(securitySession, 440 "i18nLocaleCountry", securitySession.getUser())); 441 442 pattern = MessageFormat.format(dateTimeText, arguments); 443 format = new SimpleDateFormat (pattern, locale); 444 format.setLenient(false); 445 format.setTimeZone(getTimeZone()); 446 } catch (com.ivata.mask.util.SystemException e) { 447 throw new DateFormatterException("Error in SettingDateFormatter: RemoteException " + e.getMessage()); 448 } 449 } 450 451 465 public String format(final Date date) throws DateFormatterException { 466 if ((format == null) || 467 (dateFormat == DateFormatterConstants.DATE_RELATIVE) || 468 (dateFormat == DateFormatterConstants.DATE_SHORT_RELATIVE)) { 469 try { 470 createFormat(date); 471 } catch (SettingsInitializationException e1) { 472 throw new DateFormatterException(e1); 473 } 474 } 475 String returnString = ""; 476 477 try { 478 returnString = format.format(date); 479 } catch (Exception e) { 480 throw new DateFormatterException("ERROR in SettingDateFormatter: format threw " 481 + e.getClass() 482 + " formatting '" 483 + date 484 + "', using date format '" 485 + dateFormatString 486 + "', time format '" 487 + timeFormatString 488 + "': " 489 + e.getMessage()); 490 } 491 return returnString; 492 } 493 494 502 public int getDateFormat() { 503 return dateFormat; 504 } 505 506 518 public String getDateTimeText() { 519 return dateTimeText; 520 } 521 522 530 public int getTimeFormat() { 531 return timeFormat; 532 } 533 534 543 private TimeZone getTimeZone() throws DateFormatterException { 544 assert (securitySession != null); 545 if (this.timeZone == null) { 546 try { 547 String timeZoneID = settings.getStringSetting(securitySession, 548 "i18nTimeZone", securitySession.getUser()); 549 550 timeZone = TimeZone.getTimeZone(timeZoneID); 551 } catch(SystemException e) { 552 throw new DateFormatterException("Error in SettingDateFormatter getting the timeZone: RemoteException " + e.getMessage()); 553 } 554 } 555 return timeZone; 556 } 557 558 569 public String getUserName() { 570 return userName; 571 } 572 573 589 public Date parse(final String formatString) throws ParseException , 590 DateFormatterException { 591 if ((dateFormat == DateFormatterConstants.DATE_RELATIVE) || 592 (dateFormat == DateFormatterConstants.DATE_SHORT_RELATIVE)) { 593 return null; 596 } 597 if (format == null) { 598 try { 599 createFormat(null); 600 } catch (SettingsInitializationException e1) { 601 throw new DateFormatterException(e1); 602 } 603 } 604 return format.parse(formatString); 605 } 606 607 617 public void setDateFormat(final int dateFormat) 618 throws DateFormatterException { 619 this.dateFormat = dateFormat; 620 try { 621 createFormat(null); 623 } catch (SettingsInitializationException e) { 624 throw new DateFormatterException(e); 625 } 626 } 627 628 647 public void setDateTimeText(final String dateTimeText) 648 throws DateFormatterException { 649 this.dateTimeText = dateTimeText; 650 messageFormat = null; 652 try { 653 createFormat(null); 654 } catch (SettingsInitializationException e) { 655 throw new DateFormatterException(e); 656 } 657 } 658 663 public void setSecuritySession(SecuritySession securitySessionParam) { 664 if (log.isDebugEnabled()) { 665 log.debug(toString() + " setSecuritySession before: '" 666 + securitySession + "', after: '" + securitySessionParam 667 + "'"); 668 } 669 670 securitySession = securitySessionParam; 671 } 672 673 687 public void setTimeFormat(final int timeFormat) throws DateFormatterException { 688 this.timeFormat = timeFormat; 689 try { 690 createFormat(null); 692 } catch (SettingsInitializationException e) { 693 throw new DateFormatterException(e); 694 } 695 } 696 697 719 public void setUserName(String userName) throws SettingsInitializationException, DateFormatterException { 720 this.userName = userName; 721 createFormat(null); 723 } 724 } 725 | Popular Tags |