001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org . |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013:
014: package org.rapla.storage.xml;
015:
016: import java.util.ArrayList;
017: import java.util.Collection;
018: import java.util.Date;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.rapla.entities.User;
023: import org.rapla.entities.domain.Allocatable;
024: import org.rapla.entities.domain.Appointment;
025: import org.rapla.entities.domain.Repeating;
026: import org.rapla.entities.domain.RepeatingType;
027: import org.rapla.entities.domain.internal.AppointmentImpl;
028: import org.rapla.entities.domain.internal.ReservationImpl;
029: import org.rapla.framework.RaplaContext;
030: import org.rapla.framework.RaplaException;
031: import org.xml.sax.Attributes;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.SAXParseException;
034:
035: public class ReservationReader extends RaplaXMLReader {
036: ReservationImpl reservation;
037: private Map appointmentsOldSchema = new HashMap();
038: private Object allocatableId = null;
039: private AppointmentImpl appointment = null;
040: private Repeating repeating = null;
041: private Collection restrictionsOldSchema = new ArrayList();
042:
043: private DynAttReader dynAttHandler;
044:
045: public ReservationReader(RaplaContext context)
046: throws RaplaException {
047: super (context);
048: dynAttHandler = new DynAttReader(context);
049: addChildHandler(dynAttHandler);
050: }
051:
052: public void processElement(String namespaceURI, String localName,
053: String qName, Attributes atts) throws SAXException {
054: if (namespaceURI.equals(DYNATT_NS)) {
055: dynAttHandler.setClassifiable(reservation);
056: delegateElement(dynAttHandler, namespaceURI, localName,
057: qName, atts);
058: return;
059: }
060:
061: if (!namespaceURI.equals(RAPLA_NS))
062: return;
063:
064: if (localName.equals("reservation")) {
065: String createdAt = atts.getValue("", "created-at");
066: String lastChanged = atts.getValue("", "last-changed");
067: String lastChangedBy = atts.getValue("", "last-changed-by");
068:
069: Date createTime = null;
070: Date changeTime = createTime;
071: if (createdAt != null)
072: createTime = parseDate(createdAt, false);
073: if (lastChanged != null)
074: changeTime = parseDate(lastChanged, false);
075:
076: reservation = new ReservationImpl(createTime, changeTime);
077: if (lastChangedBy != null) {
078: try {
079: User user = (User) resolve(User.TYPE, lastChangedBy);
080: reservation.setLastChangedBy(user);
081: } catch (SAXParseException ex) {
082: getLogger().warn(
083: "Can't find user " + lastChangedBy
084: + " at line " + ex.getLineNumber());
085: }
086: }
087: setId(reservation, atts);
088: setVersionIfThere(reservation, atts);
089:
090: setOwner(reservation, atts);
091: appointmentsOldSchema.clear();
092: }
093:
094: if (localName.equals("appointment")) {
095: if (atts.getValue("idref") != null) {
096: Object appointment = appointmentsOldSchema.get(atts
097: .getValue("idref"));
098: restrictionsOldSchema.add(appointment);
099: } else {
100: String id = atts.getValue("id");
101: String startDate = atts.getValue("start-date");
102: String endDate = atts.getValue("end-date");
103: if (endDate == null)
104: endDate = startDate;
105:
106: String startTime = atts.getValue("start-time");
107: String endTime = atts.getValue("end-time");
108:
109: Date start;
110: Date end;
111: if (startTime != null && endTime != null) {
112: start = parseDateTime(startDate, startTime);
113: end = parseDateTime(endDate, endTime);
114: } else {
115: start = parseDate(startDate, false);
116: end = parseDate(endDate, true);
117: }
118:
119: appointment = new AppointmentImpl(start, end);
120: appointment.setWholeDays(startTime == null
121: || endTime == null);
122: if (atts.getValue("id") != null) {
123: setId(appointment, atts);
124: } else {
125: setNewId(appointment);
126: }
127: setVersionIfThere(appointment, atts);
128: reservation.addAppointment(appointment);
129: if (id != null) {
130: appointmentsOldSchema.put(id, appointment);
131: }
132: }
133: }
134:
135: if (localName.equals("repeating")) {
136: String type = atts.getValue("type");
137: String interval = atts.getValue("interval");
138: String enddate = atts.getValue("end-date");
139: String number = atts.getValue("number");
140: appointment.setRepeatingEnabled(true);
141: repeating = appointment.getRepeating();
142: repeating.setType(RepeatingType.findForString(type));
143: if (interval != null) {
144: repeating.setInterval(Integer.valueOf(interval)
145: .intValue());
146: }
147: if (enddate != null) {
148: repeating.setEnd(parseDate(enddate, true));
149: } else if (number != null) {
150: repeating.setNumber(Integer.valueOf(number).intValue());
151: } else {
152: repeating.setEnd(null);
153: }
154: /*
155: if (getLogger().enabled(6))
156: getLogger().log(6, "Repeating " + repeating.toString() );
157: */
158: }
159:
160: if (localName.equals("allocate")) {
161: String id = getString(atts, "idref");
162: allocatableId = getId(Allocatable.TYPE, id);
163: reservation.getReferenceHandler().addId(allocatableId);
164: restrictionsOldSchema.clear();
165: if (appointment != null) {
166: reservation.addRestrictionForId(allocatableId,
167: appointment);
168: }
169: }
170:
171: if (localName.equals("exception")) {
172: }
173:
174: if (localName.equals("date")) {
175: String dateString = atts.getValue("date");
176: if (dateString != null && repeating != null)
177: repeating.addException(parseDate(dateString, false));
178: }
179: }
180:
181: public void processEnd(String namespaceURI, String localName,
182: String qName) throws SAXException {
183: if (!namespaceURI.equals(RAPLA_NS))
184: return;
185:
186: if (localName.equals("appointment") && appointment != null) {
187: add(appointment);
188: appointment = null;
189: }
190: if (localName.equals("reservation")) {
191: add(reservation);
192: }
193: if (localName.equals("allocate")) {
194: if (restrictionsOldSchema.size() > 0) {
195: Appointment[] apps = (Appointment[]) restrictionsOldSchema
196: .toArray(Appointment.EMPTY_ARRAY);
197: reservation.setRestrictionForId(allocatableId, apps);
198: }
199: }
200: }
201: }
|