Changeset 171

Show
Ignore:
Timestamp:
02/18/06 00:27:22
Author:
douglm
Message:

Fix code to get UTC for a given date or date/time value, Date only values were appearing in wrong day.

This deals with some of the problems causing events to appear in the wrong days.

There is still a problem with the various views, e.g. For a day view some events appear in more than one day tag.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/calendar3/calCore/src/org/bedework/calcore/hibernate/CalTimezonesImpl.java

    r27 r171  
    5959import org.bedework.calfacade.BwUser; 
    6060import org.bedework.calfacade.BwTimeZone; 
    61 import org.bedework.calfacade.CalFacadeBadDateException; 
    6261import org.bedework.calfacade.CalFacadeException; 
    63 import org.bedework.calfacade.CalFacadeUtil; 
    6462import org.bedework.calfacade.ifs.CalTimezones; 
    6563import org.bedework.calfacade.ifs.Calintf; 
     
    6967import net.fortuna.ical4j.model.Component; 
    7068import net.fortuna.ical4j.model.component.VTimeZone; 
    71 import net.fortuna.ical4j.model.parameter.TzId; 
    72 import net.fortuna.ical4j.model.property.DtEnd; 
    7369import net.fortuna.ical4j.model.TimeZone; 
    7470 
     
    7672import java.util.HashMap; 
    7773import java.util.Iterator; 
    78  
    79 import org.apache.log4j.Logger; 
    8074 
    8175/** Handle timezones for bedework. 
     
    8377 * @author Mike Douglass       douglm@rpi.edu 
    8478 */ 
    85 class CalTimezonesImpl implements CalTimezones { 
     79class CalTimezonesImpl extends CalTimezones { 
    8680  private Calintf cal; 
    8781  private BwRWStats stats; 
    8882 
    89   private transient Logger log; 
    90  
    9183  private boolean publicAdmin; 
    92   private boolean debug; 
    93  
    94   private String defaultTimeZoneId; 
    95   private transient TimeZone defaultTimeZone; 
    96  
    97   private static class TimezoneInfo { 
    98     TimeZone tz; 
    99  
    100     boolean publick; 
    101  
    102     /* If tz was derived from a db object, this is the data */ 
    103     VTimeZone vtz; 
    104   } 
    105  
    106   /* Map of user TimezoneInfo */ 
    107   private HashMap timezones = new HashMap(); 
    10884 
    10985  private static volatile HashMap systemTimezones = new HashMap(); 
     
    11490  CalTimezonesImpl(Calintf cal, BwStats stats, boolean publicAdmin, boolean debug) 
    11591          throws CalFacadeException { 
     92    super(debug); 
    11693    this.cal = cal; 
    11794    this.stats = (BwRWStats)stats; 
    11895    this.publicAdmin = publicAdmin; 
    119     this.debug = debug; 
    12096 
    12197    // Force fetch of timezones 
     
    136112 
    137113    TimezoneInfo tzinfo = (TimezoneInfo)timezones.get(tzid); 
     114    TimeZone tz = new TimeZone(vtz); 
    138115 
    139116    if (tzinfo == null) { 
    140       tzinfo = new TimezoneInfo(); 
    141     } 
    142  
    143     tzinfo.vtz = vtz; 
    144     tzinfo.tz = new TimeZone(vtz); 
    145     timezones.put(tzid, tzinfo); 
    146   } 
    147  
    148   public void registerTimeZone(String id, TimeZone timezone) 
    149       throws CalFacadeException { 
    150     if (debug) { 
    151       trace("register timezone with id " + id); 
    152     } 
    153  
    154     TimezoneInfo tzinfo = (TimezoneInfo)timezones.get(id); 
    155  
    156     if (tzinfo == null) { 
    157       tzinfo = new TimezoneInfo(); 
    158       tzinfo.tz = timezone; 
    159       timezones.put(id, tzinfo); 
     117      tzinfo = new TimezoneInfo(tz, vtz); 
     118      timezones.put(tzid, tzinfo); 
    160119    } else { 
    161       tzinfo.tz = timezone
     120      tzinfo.init(tz, vtz)
    162121    } 
    163122  } 
     
    177136      } 
    178137 
    179       tzinfo = new TimezoneInfo(); 
    180  
    181       tzinfo.vtz = vTimeZone; 
    182       tzinfo.tz = new TimeZone(vTimeZone); 
     138      tzinfo = new TimezoneInfo(new TimeZone(vTimeZone), vTimeZone); 
    183139      timezones.put(id, tzinfo); 
    184140    } 
    185141 
    186     if (tzinfo.publick) { 
     142    if (tzinfo.getPublick()) { 
    187143      stats.incSystemTzFetches(); 
    188144    } 
    189145 
    190146    if ((defaultTimeZone == null) && id.equals(defaultTimeZoneId)) { 
    191       defaultTimeZone = tzinfo.tz; 
    192     } 
    193  
    194     return tzinfo.tz; 
    195   } 
    196  
    197   public TimeZone getDefaultTimeZone() throws CalFacadeException { 
    198     if ((defaultTimeZone == null) && (defaultTimeZoneId != null)) { 
    199       defaultTimeZone = getTimeZone(defaultTimeZoneId); 
    200     } 
    201  
    202     return defaultTimeZone; 
    203   } 
    204  
    205   public void setDefaultTimeZoneId(String id) throws CalFacadeException { 
    206     defaultTimeZone = null; 
    207     defaultTimeZoneId = id; 
    208   } 
    209  
    210   public String getDefaultTimeZoneId() throws CalFacadeException { 
    211     return defaultTimeZoneId; 
     147      defaultTimeZone = tzinfo.getTz(); 
     148    } 
     149 
     150    return tzinfo.getTz(); 
    212151  } 
    213152 
     
    219158    TimezoneInfo tzinfo = lookup(id); 
    220159 
    221     if ((tzinfo != null) && (tzinfo.vtz != null)) { 
    222       return tzinfo.vtz
     160    if ((tzinfo != null) && (tzinfo.getVtz() != null)) { 
     161      return tzinfo.getVtz()
    223162    } 
    224163 
     
    228167    } 
    229168 
    230     tzinfo = new TimezoneInfo(); 
    231  
    232     tzinfo.vtz = vTimeZone; 
    233     tzinfo.tz = new TimeZone(vTimeZone); 
     169    tzinfo = new TimezoneInfo(new TimeZone(vTimeZone), vTimeZone); 
    234170    timezones.put(id, tzinfo); 
    235171 
     
    249185    // force refresh now 
    250186    lookup("not-a-timezone"); 
    251   } 
    252  
    253   public String getUtc(String time, String tzid, TimeZone tz) throws CalFacadeException { 
    254     //if (debug) { 
    255     //  trace("Get utc for " + time + " tzid=" + tzid + " tz =" + tz); 
    256     //} 
    257     if (CalFacadeUtil.isISODateTimeUTC(time)) { 
    258       // Already UTC 
    259       return time; 
    260     } 
    261  
    262     if (CalFacadeUtil.isISODateTime(time)) { 
    263       try { 
    264         DtEnd dte = new DtEnd(); 
    265  
    266         if ((tz == null) && (tzid != null)) { 
    267           tz = getTimeZone(tzid); 
    268  
    269           //if (debug) { 
    270           //  trace("--------Got timezone " + tz); 
    271           //} 
    272  
    273           if (tz == null) { 
    274             throw new CalFacadeBadDateException(); 
    275           } 
    276         } else if ((tz != null) && (tzid == null)) { 
    277           tzid = tz.getID(); 
    278         } 
    279  
    280         if (tzid != null) { 
    281           dte.getParameters().add(new TzId(tzid)); 
    282           dte.setTimeZone(tz); 
    283         } 
    284  
    285         dte.setValue(time); 
    286         dte.setUtc(true); 
    287  
    288         return dte.getValue(); 
    289       } catch (Throwable t) { 
    290         t.printStackTrace(); 
    291         throw new CalFacadeBadDateException(); 
    292       } 
    293     } 
    294  
    295     if (CalFacadeUtil.isISODate(time)) { 
    296       return time + "T000000Z"; 
    297     } 
    298  
    299     throw new CalFacadeBadDateException(); 
    300187  } 
    301188 
     
    324211            } 
    325212 
    326             tzinfo = new TimezoneInfo(); 
    327  
    328             tzinfo.vtz = vtz; 
    329             tzinfo.tz = new TimeZone(vtz); 
     213            tzinfo = new TimezoneInfo(new TimeZone(vtz), vtz, true); 
    330214            systemTimezones.put(btz.getTzid(), tzinfo); 
    331215          } 
     
    338222    tzinfo = (TimezoneInfo)systemTimezones.get(id); 
    339223 
    340     if (tzinfo != null) { 
    341       tzinfo.publick = true; 
    342     } else { 
     224    if (tzinfo == null) { 
    343225      tzinfo = (TimezoneInfo)timezones.get(id); 
    344226    } 
     
    346228    return tzinfo; 
    347229  } 
    348  
    349   /* Get a logger for messages 
    350    */ 
    351   private Logger getLogger() { 
    352     if (log == null) { 
    353       log = Logger.getLogger(this.getClass()); 
    354     } 
    355  
    356     return log; 
    357   } 
    358  
    359   private void trace(String msg) { 
    360     getLogger().debug("trace: " + msg); 
    361   } 
    362230} 
    363231 
  • trunk/calendar3/calFacade/src/org/bedework/calfacade/ifs/CalTimezones.java

    r27 r171  
    5555package org.bedework.calfacade.ifs; 
    5656 
     57import org.apache.log4j.Logger; 
    5758import org.bedework.calfacade.BwUser; 
     59import org.bedework.calfacade.CalFacadeBadDateException; 
    5860import org.bedework.calfacade.CalFacadeException; 
     61import org.bedework.calfacade.CalFacadeUtil; 
    5962 
    6063import net.fortuna.ical4j.model.TimeZone; 
    6164import net.fortuna.ical4j.model.component.VTimeZone; 
     65import net.fortuna.ical4j.util.TimeZones; 
    6266 
    6367import java.io.Serializable; 
     68import java.text.DateFormat; 
     69import java.text.SimpleDateFormat; 
     70import java.util.Calendar; 
     71import java.util.HashMap; 
    6472 
    6573/** Handle caching, retrieval and registration of timezones. 
     
    6876 * 
    6977 */ 
    70 public interface CalTimezones extends Serializable { 
     78public abstract class CalTimezones implements Serializable { 
     79  private transient Logger log; 
     80   
     81  protected boolean debug; 
     82 
     83  protected String defaultTimeZoneId; 
     84  protected transient TimeZone defaultTimeZone; 
     85 
     86  protected static class TimezoneInfo { 
     87    TimeZone tz; 
     88 
     89    /* If tz was derived from a db object, this is the data */ 
     90    VTimeZone vtz; 
     91 
     92    boolean publick; 
     93     
     94    /** 
     95     * @param tz 
     96     * @param vtz 
     97     */ 
     98    public TimezoneInfo(TimeZone tz, VTimeZone vtz) { 
     99      init(tz, vtz); 
     100    } 
     101     
     102    /** Constructor 
     103     *  
     104     * @param tz 
     105     * @param vtz 
     106     * @param publick 
     107     */ 
     108    public TimezoneInfo(TimeZone tz, VTimeZone vtz, boolean publick) { 
     109      this.tz = tz; 
     110      this.vtz = vtz; 
     111      this.publick = publick; 
     112    } 
     113     
     114    /** (Re)init the object 
     115     *  
     116     * @param tz 
     117     * @param vtz 
     118     */ 
     119    public void init(TimeZone tz, VTimeZone vtz) { 
     120      this.tz = tz; 
     121      this.vtz = vtz; 
     122    } 
     123     
     124    public TimeZone getTz() { 
     125      return tz; 
     126    } 
     127     
     128    public VTimeZone getVtz() { 
     129      return vtz; 
     130    } 
     131     
     132    /** 
     133     * @return true for public timezone 
     134     */ 
     135    public boolean getPublick() { 
     136      return publick; 
     137    } 
     138  } 
     139 
     140  /* Map of user TimezoneInfo */ 
     141  protected HashMap timezones = new HashMap(); 
     142 
     143  protected CalTimezones(boolean debug) { 
     144    this.debug = debug; 
     145  } 
     146   
    71147  /** Save a timezone definition in the database. The timezone is in the 
    72148   * form of a complete calendar definition containing a single VTimeZone 
     
    79155   * @throws CalFacadeException 
    80156   */ 
    81   public void saveTimeZone(String tzid, VTimeZone vtz) 
     157  public abstract void saveTimeZone(String tzid, VTimeZone vtz) 
    82158          throws CalFacadeException; 
    83159 
     
    88164  * @throws CalFacadeException 
    89165  */ 
    90  public void registerTimeZone(String id, TimeZone timezone) 
    91      throws CalFacadeException; 
     166  public void registerTimeZone(String id, TimeZone timezone) 
     167          throws CalFacadeException { 
     168    if (debug) { 
     169      trace("register timezone with id " + id); 
     170    } 
     171     
     172    TimezoneInfo tzinfo = (TimezoneInfo)timezones.get(id); 
     173     
     174    if (tzinfo == null) { 
     175      tzinfo = new TimezoneInfo(timezone, null); 
     176      timezones.put(id, tzinfo); 
     177    } else { 
     178      tzinfo.tz = timezone; 
     179    } 
     180  } 
    92181 
    93182 /** Get a timezone object given the id. This will return transient objects 
     
    98187  * @throws CalFacadeException 
    99188  */ 
    100  public TimeZone getTimeZone(final String id) throws CalFacadeException; 
    101  
    102  /** Get the default timezone for this system. 
    103   * 
    104   * @return default TimeZone or null for none set. 
    105   * @throws CalFacadeException 
    106   */ 
    107  public TimeZone getDefaultTimeZone() throws CalFacadeException; 
    108  
    109  /** Set the default timezone id for this system. 
    110   * 
    111   * @param id 
    112   * @throws CalFacadeException 
    113   */ 
    114  public void setDefaultTimeZoneId(String id) throws CalFacadeException; 
    115  
    116  /** Get the default timezone id for this system. 
    117  * 
    118  * @return String id 
    119  * @throws CalFacadeException 
    120  */ 
    121  public String getDefaultTimeZoneId() throws CalFacadeException; 
    122  
    123  /** Find a timezone object in the database given the id. 
    124   * 
    125   * @param id 
    126   * @param owner     event owner or null for current user 
    127   * @return VTimeZone with id or null 
    128   * @throws CalFacadeException 
    129   */ 
    130  public VTimeZone findTimeZone(final String id, BwUser owner) throws CalFacadeException; 
    131  
    132  /** Clear all public timezone objects 
    133   * 
    134   * <p>Will remove all public timezones in preparation for a replacement 
    135   * (presumably) 
    136   * 
    137   * @throws CalFacadeException 
    138   */ 
    139  public void clearPublicTimezones() throws CalFacadeException; 
    140  
    141  /** Refresh the public timezone table - presumably after a call to clearPublicTimezones. 
    142   * and many calls to saveTimeZone. 
    143   * 
    144   * @throws CalFacadeException 
    145   */ 
    146  public void refreshTimezones() throws CalFacadeException; 
    147  
    148  /** Given a String time value and a possibly null tzid and/or timezone 
    149   *  will return a UTC formatted value. The supplied time should be of the 
    150   *  form yyyyMMdd or yyyyMMddThhmmss or yyyyMMddThhmmssZ 
    151   * 
    152   *  <p>The last form will be returned untouched, the first will have T000000Z 
    153   *  appended and the second will be converted to the equivalent UTC time. 
    154   * 
    155   *  <p>The returned value is used internally as a value for indexes and 
    156   *  recurrence ids. 
    157   * 
    158   *  <p>Both tzid and tz null mean this is local or floating time 
    159   * 
    160   * @param time  String time to convert. 
    161   * @param tzid  String tzid. 
    162   * @param tz    If set used in preference to tzid. 
    163   * @return String always of form yyyyMMddThhmmssZ 
    164   * @throws CalFacadeException for bad parameters or timezone 
    165   */ 
    166  public String getUtc(String time, String tzid, TimeZone tz) throws CalFacadeException; 
     189  public abstract TimeZone getTimeZone(final String id) throws CalFacadeException; 
     190   
     191  /** Get the default timezone for this system. 
     192   * 
     193   * @return default TimeZone or null for none set. 
     194   * @throws CalFacadeException 
     195   */ 
     196  public TimeZone getDefaultTimeZone() throws CalFacadeException { 
     197    if ((defaultTimeZone == null) && (defaultTimeZoneId != null)) { 
     198      defaultTimeZone = getTimeZone(defaultTimeZoneId); 
     199    } 
     200 
     201    return defaultTimeZone; 
     202  } 
     203   
     204  /** Set the default timezone id for this system. 
     205   * 
     206   * @param id 
     207   * @throws CalFacadeException 
     208   */ 
     209  public void setDefaultTimeZoneId(String id) throws CalFacadeException { 
     210    defaultTimeZone = null; 
     211    defaultTimeZoneId = id; 
     212  } 
     213   
     214  /** Get the default timezone id for this system. 
     215   * 
     216   * @return String id 
     217   * @throws CalFacadeException 
     218   */ 
     219  public String getDefaultTimeZoneId() throws CalFacadeException { 
     220    return defaultTimeZoneId; 
     221  } 
     222   
     223  /** Find a timezone object in the database given the id. 
     224   * 
     225   * @param id 
     226   * @param owner     event owner or null for current user 
     227   * @return VTimeZone with id or null 
     228   * @throws CalFacadeException 
     229   */ 
     230  public abstract VTimeZone findTimeZone(final String id, BwUser owner) throws CalFacadeException; 
     231   
     232  /** Clear all public timezone objects 
     233   * 
     234   * <p>Will remove all public timezones in preparation for a replacement 
     235   * (presumably) 
     236   * 
     237   * @throws CalFacadeException 
     238   */ 
     239  public abstract void clearPublicTimezones() throws CalFacadeException; 
     240   
     241  /** Refresh the public timezone table - presumably after a call to clearPublicTimezones. 
     242   * and many calls to saveTimeZone. 
     243   * 
     244   * @throws CalFacadeException 
     245   */ 
     246  public abstract void refreshTimezones() throws CalFacadeException; 
     247   
     248  private static DateFormat formatTd  = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); 
     249  private static Calendar cal = Calendar.getInstance(); 
     250  private static java.util.TimeZone utctz; 
     251  private static java.util.TimeZone lasttz; 
     252  private static String lasttzid; 
     253  static { 
     254    try { 
     255      utctz = TimeZone.getTimeZone(TimeZones.UTC_ID); 
     256    } catch (Throwable t) { 
     257      throw new RuntimeException("Unable to initialise UTC timezone"); 
     258    } 
     259    cal.setTimeZone(utctz); 
     260  } 
     261   
     262  /** Given a String time value and a possibly null tzid and/or timezone 
     263   *  will return a UTC formatted value. The supplied time should be of the 
     264   *  form yyyyMMdd or yyyyMMddThhmmss or yyyyMMddThhmmssZ 
     265   * 
     266   *  <p>The last form will be returned untouched, it's already UTC. 
     267   *   
     268   *  <p>the first will have T000000 appended to the parameter value then the 
     269   *  first and second will be converted to the equivalent UTC time. 
     270   * 
     271   *  <p>The returned value is used internally as a value for indexes and 
     272   *  recurrence ids. 
     273   * 
     274   *  <p>Both tzid and tz null mean this is local or floating time 
     275   * 
     276   * @param time  String time to convert. 
     277   * @param tzid  String tzid. 
     278   * @param tz    If set used in preference to tzid. 
     279   * @return String always of form yyyyMMddThhmmssZ 
     280   * @throws CalFacadeException for bad parameters or timezone 
     281   */ 
     282  public synchronized String getUtc(String time, String tzid, TimeZone tz)  
     283  throws CalFacadeException { 
     284    /* XXX We probably need the ownerid to determine exactly which timezone 
     285     */ 
     286    //if (debug) { 
     287    //  trace("Get utc for " + time + " tzid=" + tzid + " tz =" + tz); 
     288    //} 
     289    if (CalFacadeUtil.isISODateTimeUTC(time)) { 
     290      // Already UTC 
     291      return time; 
     292    } 
     293     
     294    if (CalFacadeUtil.isISODate(time)) { 
     295      time += "T000000"; 
     296    } else if (!CalFacadeUtil.isISODateTime(time)) { 
     297      throw new CalFacadeBadDateException(); 
     298    } 
     299     
     300    try { 
     301      boolean tzchanged = false; 
     302       
     303      if (tz == null) { 
     304        if (tzid == null) { 
     305          if ((lasttzid != null) || (lasttz == null)) { 
     306            lasttz = TimeZone.getDefault(); 
     307            tzchanged = true; 
     308          } 
     309        } else { 
     310          if ((lasttzid == null) || (!lasttzid.equals(tzid))) { 
     311            lasttz = getTimeZone(tzid); 
     312            if (lasttz == null) { 
     313              lasttzid = null; 
     314              throw new CalFacadeBadDateException(); 
     315            } 
     316            tzchanged = true; 
     317          } 
     318        } 
     319      } else { 
     320        // tz supplied 
     321        if (tz != lasttz) { 
     322          /* Yes, that's a !=. I'm looking for it being the same object. 
     323           * If I were sure that equals were correct and fast I'd use 
     324           * that. 
     325           */ 
     326          tzchanged = true; 
     327          tzid = tz.getID(); 
     328          lasttz = tz; 
     329        } 
     330      } 
     331       
     332       
     333      if (tzchanged) { 
     334        if (debug) { 
     335          trace("**********tzchanged for tzid " + tzid); 
     336        } 
     337        formatTd.setTimeZone(lasttz); 
     338        lasttzid = tzid; 
     339      } 
     340       
     341      cal.setTime(formatTd.parse(time)); 
     342       
     343      StringBuffer sb = new StringBuffer(); 
     344      digit4(sb, cal.get(Calendar.YEAR)); 
     345      digit2(sb, cal.get(Calendar.MONTH) + 1); // Month starts at 0 
     346      digit2(sb, cal.get(Calendar.DAY_OF_MONTH)); 
     347      sb.append('T'); 
     348      digit2(sb, cal.get(Calendar.HOUR_OF_DAY)); 
     349      digit2(sb, cal.get(Calendar.MINUTE)); 
     350      digit2(sb, cal.get(Calendar.SECOND)); 
     351      sb.append('Z'); 
     352      return sb.toString(); 
     353    } catch (Throwable t) { 
     354      t.printStackTrace(); 
     355      throw new CalFacadeBadDateException(); 
     356    } 
     357  } 
     358   
     359  /* ==================================================================== 
     360   *                   protected methods 
     361   * ==================================================================== */ 
     362   
     363  protected void digit2(StringBuffer sb, int val) throws CalFacadeException { 
     364    if (val > 99) { 
     365      throw new CalFacadeBadDateException(); 
     366    } 
     367    if (val < 10) { 
     368      sb.append("0"); 
     369    } 
     370    sb.append(val); 
     371  } 
     372   
     373  protected void digit4(StringBuffer sb, int val) throws CalFacadeException { 
     374    if (val > 9999) { 
     375      throw new CalFacadeBadDateException(); 
     376    } 
     377    if (val < 10) { 
     378      sb.append("000"); 
     379    } else if (val < 100) { 
     380      sb.append("00"); 
     381    } else if (val < 1000) { 
     382      sb.append("0"); 
     383    } 
     384    sb.append(val); 
     385  } 
     386   
     387 
     388  /* Get a logger for messages 
     389   */ 
     390  protected Logger getLogger() { 
     391    if (log == null) { 
     392      log = Logger.getLogger(this.getClass()); 
     393    } 
     394 
     395    return log; 
     396  } 
     397 
     398  protected void trace(String msg) { 
     399    getLogger().debug("trace: " + msg); 
     400  } 
    167401} 
  • trunk/calendar3/docs/todo.txt

    r170 r171  
    1919 
    2020Switch displays and ensure they turn up. 
     21-------------------------------------------------------------------------------- 
     22Restore makes a date type then turns it into datetime. Screws tz caching. 
     23Save values and fix up at end? 
     24-------------------------------------------------------------------------------- 
     252.3.2 dump needs to dump filters before events. 
    2126-------------------------------------------------------------------------------- 
    2227update quickstart stuff 
  • trunk/calendar3/dumprestore/build.xml

    r75 r171  
    262262      <arg value="-usergroupsClass" /> 
    263263      <arg value="${org.bedework.syspar.usergroupsclass}" /> 
     264      <jvmarg value="-mx128m" /> 
     265      <jvmarg value="-ms128m" /> 
    264266      <!-- 
    265267      <jvmarg value="-Xrunyjpagent:cpu=times,onexit=cpu" /> 
  • trunk/calendar3/dumprestore/src/org/bedework/dumprestore/restore/TimezonesImpl.java

    r99 r171  
    5757import org.bedework.calfacade.BwUser; 
    5858import org.bedework.calfacade.BwTimeZone; 
    59 import org.bedework.calfacade.CalFacadeBadDateException; 
    6059import org.bedework.calfacade.CalFacadeException; 
    61 import org.bedework.calfacade.CalFacadeUtil; 
    6260import org.bedework.calfacade.ifs.CalTimezones; 
    6361 
    6462import net.fortuna.ical4j.model.component.VTimeZone; 
    6563import net.fortuna.ical4j.model.TimeZone; 
    66 import net.fortuna.ical4j.util.TimeZones; 
    67  
    68 import java.text.DateFormat; 
    69 import java.text.SimpleDateFormat; 
    70 import java.util.Calendar; 
    71 import java.util.HashMap; 
    72  
    73 import org.apache.log4j.Logger; 
    7464 
    7565/** Handle timezones for bedework restore program. 
    76  * 
    77  * <p>At the moment this does nothing as we have no timezones in the 2.3 
    78  * version. This needs to act as a repository for all timezones as we restore 
    79  * them. 
    8066 * 
    8167 * @author Mike Douglass       douglm@rpi.edu 
    8268 */ 
    83 class TimezonesImpl implements CalTimezones { 
    84   private transient Logger log; 
    85  
    86   private boolean debug; 
     69class TimezonesImpl extends CalTimezones { 
    8770  private boolean publick = true; // current mode 
    8871  private BwUser user; 
    8972 
    90   private String defaultTimeZoneId; 
    91   private transient TimeZone defaultTimeZone; 
    92  
    9373  private RestoreIntf ri; 
    9474 
    95   private static class TimezoneInfo { 
    96     TimeZone tz; 
    97  
    98     boolean publick; 
    99  
    100     /* If tz was derived from a db object, this is the data */ 
    101     VTimeZone vtz; 
    102   } 
    103  
    104   /* Map of user TimezoneInfo */ 
    105   private HashMap timezones = new HashMap(); 
    106  
    107   private static volatile HashMap systemTimezones = new HashMap(); 
     75//  private static volatile HashMap systemTimezones = new HashMap(); 
    10876  //private static volatile boolean systemTimezonesInitialised = false; 
    109  
    110   //private transient Logger log; 
    11177 
    11278  TimezonesImpl(boolean debug, BwUser user, RestoreIntf ri) 
    11379          throws CalFacadeException { 
    114     this.debug = debug
     80    super(debug)
    11581    this.user = user; 
    11682    this.ri = ri; 
     
    142108    */ 
    143109 
    144     BwTimeZone tz = new BwTimeZone(); 
    145  
    146     tz.setTzid(tzid); 
    147     tz.setPublick(publick); 
    148     tz.setOwner(user); 
     110    BwTimeZone btz = new BwTimeZone(); 
     111 
     112    btz.setTzid(tzid); 
     113    btz.setPublick(publick); 
     114    btz.setOwner(user); 
    149115 
    150116    StringBuffer sb = new StringBuffer(); 
     
    156122    sb.append("END:VCALENDAR\n"); 
    157123 
    158     tz.setVtimezone(sb.toString()); 
     124    btz.setVtimezone(sb.toString()); 
    159125 
    160126    try { 
    161       ri.restoreTimezone(tz); 
     127      ri.restoreTimezone(btz); 
    162128    } catch (Throwable t) { 
    163129      throw new CalFacadeException(t); 
     
    165131 
    166132    TimezoneInfo tzinfo = (TimezoneInfo)timezones.get(tzid); 
     133    TimeZone tz = new TimeZone(vtz); 
    167134 
    168135    if (tzinfo == null) { 
    169       tzinfo = new TimezoneInfo(); 
    170     } 
    171  
    172     tzinfo.vtz = vtz; 
    173     tzinfo.tz = new TimeZone(vtz); 
    174     timezones.put(tzid, tzinfo); 
    175   } 
    176  
    177   public void registerTimeZone(String id, TimeZone timezone) 
    178       throws CalFacadeException { 
    179     if (debug) { 
    180       trace("register timezone with id " + id); 
    181     } 
    182  
    183     TimezoneInfo tzinfo = (TimezoneInfo)timezones.get(id); 
    184  
    185     if (tzinfo == null) { 
    186       tzinfo = new TimezoneInfo(); 
    187       tzinfo.tz = timezone; 
    188       timezones.put(id, tzinfo); 
     136      tzinfo = new TimezoneInfo(tz, vtz); 
     137      timezones.put(tzid, tzinfo); 
    189138    } else { 
    190       tzinfo.tz = timezone
     139      tzinfo.init(tz, vtz)
    191140    } 
    192141  } 
     
    210159    */ 
    211160 
    212     return tzinfo.tz; 
    213   } 
    214  
    215   public TimeZone getDefaultTimeZone() throws CalFacadeException { 
    216     if ((defaultTimeZone == null) && (defaultTimeZoneId != null)) { 
    217       defaultTimeZone = getTimeZone(defaultTimeZoneId); 
    218     } 
    219  
    220     return defaultTimeZone; 
    221   } 
    222  
    223   public void setDefaultTimeZoneId(String id) throws CalFacadeException { 
    224     defaultTimeZone = null; 
    225     defaultTimeZoneId = id; 
    226   } 
    227  
    228   public String getDefaultTimeZoneId() throws CalFacadeException { 
    229     return defaultTimeZoneId; 
     161    return tzinfo.getTz(); 
    230162  } 
    231163 
     
    237169    TimezoneInfo tzinfo = lookup(id); 
    238170 
    239     if ((tzinfo != null) && (tzinfo.vtz != null)) { 
    240       return tzinfo.vtz
     171    if ((tzinfo != null) && (tzinfo.getVtz() != null)) { 
     172      return tzinfo.getVtz()
    241173    } 
    242174 
     
    264196    synchronized (this) { 
    265197      //systemTimezonesInitialised = false; 
    266       systemTimezones = new HashMap(); 
     198      // systemTimezones = new HashMap(); 
    267199    } 
    268200 
     
    270202    lookup("not-a-timezone"); 
    271203  } 
    272  
    273   private static DateFormat formatTd  = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); 
    274   private static Calendar cal = Calendar.getInstance(); 
    275   private static java.util.TimeZone utctz; 
    276   private static java.util.TimeZone lasttz; 
    277   private static String lasttzid; 
    278   static { 
    279     try { 
    280       utctz = TimeZone.getTimeZone(TimeZones.UTC_ID); 
    281     } catch (Throwable t) { 
    282       throw new RuntimeException("Unable to initialise UTC timezone"); 
    283     } 
    284     cal.setTimeZone(utctz); 
    285   } 
    286  
    287   public synchronized String getUtc(String time, String tzid, TimeZone tz) throws CalFacadeException { 
    288     /* XXX We probably need the ownerid to determine exactly which timezone 
    289      */ 
    290     //if (debug) { 
    291     //  trace("Get utc for " + time + " tzid=" + tzid + " tz =" + tz); 
    292     //} 
    293     if (CalFacadeUtil.isISODateTimeUTC(time)) { 
    294       // Already UTC 
    295       return time; 
    296     } 
    297  
    298     if (CalFacadeUtil.isISODateTime(time)) { 
    299       try { 
    300         boolean tzchanged = false; 
    301  
    302         if (tz == null) { 
    303           if (tzid == null) { 
    304             if ((lasttzid != null) || (lasttz == null)) { 
    305               lasttz = TimeZone.getDefault(); 
    306               tzchanged = true; 
    307             } 
    308           } else { 
    309             if ((lasttzid == null) || (!lasttzid.equals(tzid))) { 
    310               lasttz = getTimeZone(tzid); 
    311               if (lasttz == null) { 
    312                 lasttzid = null; 
    313                 throw new CalFacadeBadDateException(); 
    314               } 
    315               tzchanged = true; 
    316             } 
    317           } 
    318         } else { 
    319           // tz supplied 
    320           if (tz != lasttz) { 
    321             /* Yes, that's a !=. I'm looking for it being the same object. 
    322              * If I were sure that equals were correct and fast I'd use 
    323              * that. 
    324              */ 
    325             tzchanged = true; 
    326             tzid = tz.getID(); 
    327             lasttz = tz; 
    328           } 
    329         } 
    330  
    331  
    332         if (tzchanged) { 
    333           if (debug) { 
    334             trace("**********tzchanged"); 
    335           } 
    336           formatTd.setTimeZone(lasttz); 
    337           lasttzid = tzid; 
    338         } 
    339  
    340         cal.setTime(formatTd.parse(time)); 
    341  
    342         StringBuffer sb = new StringBuffer(); 
    343         digit4(sb, cal.get(Calendar.YEAR)); 
    344         digit2(sb, cal.get(Calendar.MONTH) + 1); // Month starts at 0 
    345         digit2(sb, cal.get(Calendar.DAY_OF_MONTH)); 
    346         sb.append('T'); 
    347         digit2(sb, cal.get(Calendar.HOUR_OF_DAY)); 
    348         digit2(sb, cal.get(Calendar.MINUTE)); 
    349         digit2(sb, cal.get(Calendar.SECOND)); 
    350         sb.append('Z'); 
    351         return sb.toString(); 
    352       } catch (Throwable t) { 
    353         t.printStackTrace(); 
    354         throw new CalFacadeBadDateException(); 
    355       } 
    356     } 
    357  
    358     if (CalFacadeUtil.isISODate(time)) { 
    359       return time + "T000000Z"; 
    360     } 
    361  
    362     throw new CalFacadeBadDateException(); 
    363   } 
    364  
     204   
    365205  /* ==================================================================== 
    366206   *                   Private methods 
    367207   * ==================================================================== */ 
    368  
    369   private void digit2(StringBuffer sb, int val) throws CalFacadeException { 
    370     if (val > 99) { 
    371       throw new CalFacadeBadDateException(); 
    372     } 
    373     if (val < 10) { 
    374       sb.append("0"); 
    375     } 
    376     sb.append(val); 
    377   } 
    378  
    379   private void digit4(StringBuffer sb, int val) throws CalFacadeException { 
    380     if (val > 9999) { 
    381       throw new CalFacadeBadDateException(); 
    382     } 
    383     if (val < 10) { 
    384       sb.append("000"); 
    385     } else if (val < 100) { 
    386       sb.append("00"); 
    387     } else if (val < 1000) { 
    388       sb.append("0"); 
    389     } 
    390     sb.append(val); 
    391   } 
    392208 
    393209  private TimezoneInfo lookup(String id) throws CalFacadeException { 
     
    425241    */ 
    426242 
    427     tzinfo = (TimezoneInfo)systemTimezones.get(id); 
    428  
    429     if (tzinfo != null) { 
    430       tzinfo.publick = true; 
    431     } else { 
     243//    tzinfo = (TimezoneInfo)systemTimezones.get(id); 
     244 
     245  //  if (tzinfo != null) { 
     246  //    tzinfo.publick = true; 
     247  //  } else { 
    432248      tzinfo = (TimezoneInfo)timezones.get(id); 
    433     } 
     249  //  } 
    434250 
    435251    return tzinfo; 
    436252  } 
    437  
    438   /* Get a logger for messages 
    439    */ 
    440   private Logger getLogger() { 
    441     if (log == null) { 
    442       log = Logger.getLogger(this.getClass()); 
    443     } 
    444  
    445     return log; 
    446   } 
    447  
    448   private void trace(String msg) { 
    449     getLogger().debug("trace: " + msg); 
    450   } 
    451253} 
  • trunk/calendar3/dumprestore/src/org/bedework/dumprestore/restore/rules/FilterRule.java

    r75 r171  
    108108          globals.calLeaves.add(entity); 
    109109          globals.catCalTbl.put(catf.getCategory().getId(), cal); 
     110          if (globals.debug) { 
     111            trace("Save calendar with id " + cal.getId()); 
     112          } 
    110113        } else if (entity instanceof BwCreatorFilter) { 
    111114          cal.setCalendarCollection(true); 
     
    137140          globals.calMapErrors++; 
    138141        } else if (entity instanceof RestoreGlobals.AliasFilter) { 
     142          error("Unable to map filter " + entity); 
     143          cal = null; 
     144          globals.calMapErrors++; 
     145        } else { 
    139146          error("Unable to map filter " + entity); 
    140147          cal = null;