Catch throwables, not just exceptions (catches contact parser errors)
[funambol-groupdav-connector.git] / src / main / java / net / bionicmessage / funambol / groupdav / notes / InboundFunambolNoteObject.java
blob7ee1d79e457b97c80340104f6bd80597406e41ab
1 /* InboundFunambolNoteObject.java
2 *
3 * Created on Apr 28 2009, 12:12:12 UTC
4 *
5 * Copyright (C) 2009 credativ GmbH
6 *
7 * GroupDAV connector for Funambol v6.5
8 * Heavily based on InboundFunambolContactObject.java [Copyright (C) 2007 Mathew McBride]
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package net.bionicmessage.funambol.groupdav.notes;
25 import com.funambol.common.pim.note.Note;
26 import net.bionicmessage.funambol.datakit.NoteConverter;
27 import net.bionicmessage.funambol.framework.InboundObject;
28 import com.funambol.common.pim.converter.BaseConverter;
29 import com.funambol.common.pim.converter.NoteToSIFN;
30 import com.funambol.common.pim.sif.SIFNParser;
31 import com.funambol.common.pim.vnote.VNoteParser;
32 import com.funambol.framework.engine.SyncItem;
33 import com.funambol.framework.engine.source.SyncContext;
34 import com.funambol.common.pim.common.Property;
35 import java.io.ByteArrayInputStream;
36 import java.io.UnsupportedEncodingException;
37 import java.util.logging.Logger;
38 import net.bionicmessage.funambol.framework.Constants;
39 import net.bionicmessage.funambol.framework.ObjectTransformationException;
40 import net.bionicmessage.funambol.groupdav.calendar.iCalSourceObject;
41 import net.bionicmessage.utils.QPDecode;
42 import net.fortuna.ical4j.data.CalendarBuilder;
43 import net.fortuna.ical4j.model.Calendar;
45 /**
46 * Represents a Note object inbound to NoteSyncSource.
47 * @author Marc Brockschmidt
49 public final class InboundFunambolNoteObject extends iCalSourceObject implements InboundObject {
50 /**
51 * Identifier of the destination store.
53 private String destinationStore = "default";
55 /**
56 * Inbound note.
58 private Note noteObject = null;
60 /**
61 * Uid of this note.
63 private String uid = null;
65 /**
66 * Sync item (original one).
68 private SyncItem origItem;
70 /**
71 * Logging object used throughout the code.
73 private Logger log = null;
75 /**
76 * Sync context of this synchronization source.
78 private SyncContext syncContext;
80 /**
81 * Default constructor for InboundFunambolNoteObject, creating an
82 * object based on a sync item.
83 * @param si incoming sync item.
84 * @param l Logger object used throughout the code.
86 public InboundFunambolNoteObject(final SyncItem si, final Logger l) {
87 origItem = si;
88 log = l;
91 /**
92 * @param id unique ID of the encapsulated note object
94 public void setUid(final String id) {
95 this.uid = id;
96 if (noteObject != null) {
97 if (noteObject.getUid() != null) {
98 noteObject.getUid().setPropertyValue(id);
99 } else {
100 noteObject.setUid(new Property(id));
102 } else {
103 log.info("Trying to set uid " + uid + " on null note object!");
108 * @return unique ID of the encapsulated note object
110 public String getUid() {
111 return uid;
115 * @param destination identifier of the store for this inbound object.
117 public void setDestinationStore(final String destination) {
118 destinationStore = destination;
122 * @return identifier of the store for this inbound object.
124 public String getDestinationStore() {
125 return destinationStore;
128 @Override
129 public Object getPDIDataObject() {
130 return noteObject;
133 @Override
134 public Class getPDIDataClass() {
135 return com.funambol.common.pim.note.Note.class;
138 @Override
139 public String getDefaultStringOutputType() {
140 return Constants.TYPE_NOTE_ICAL;
143 /**
144 * Return the note subject for this subject.
145 * @return A String with the note subject
147 public String getSummary() {
148 return noteObject.getTextDescription().getPropertyValueAsString();
152 * @return sync context of this inbound note object
154 public SyncContext getSyncContext() {
155 return syncContext;
159 * @param ctx sync context of this inbound note object
161 public void setSyncContext(final SyncContext ctx) {
162 this.syncContext = ctx;
165 @Override
166 public String getStringRepresentation(final String type) throws ObjectTransformationException {
167 try {
168 if (type.equals(Constants.TYPE_NOTE_SIFN)) {
169 BaseConverter conv = new NoteToSIFN(null, BaseConverter.CHARSET_UTF8);
170 return conv.convert(noteObject);
171 } else if (type.endsWith(Constants.TYPE_NOTE_VNOTE)) {
172 return NoteConverter.convertNote2VNote(noteObject).toString();
173 } else if (type.equals(Constants.TYPE_NOTE_ICAL)) {
174 return NoteConverter.convertNote2ical4j(noteObject, null).toString();
175 } else if (type.equals(Constants.TYPE_NOTE_PLAIN)) {
176 return NoteConverter.convertNote2Plain(noteObject);
178 } catch (Exception e) {
179 throw new ObjectTransformationException("Error transforming contact to" + type, e);
181 throw new UnsupportedOperationException("Type: " + type + " unsupported");
184 @Override
185 public void parseIntoObjectFromString(final String str, final String type) throws ObjectTransformationException {
186 try {
187 parseIntoObjectFromBytes(str.getBytes("UTF-8"), type);
188 } catch (UnsupportedEncodingException e) {
189 throw new ObjectTransformationException("Error outputting content in UTF-8");
194 * Parse byte array into a funambol Note object.
196 * @param content array of bytes representing the decoded input
197 * @param type data type of the input.
198 * @throws ObjectTransformationException if conversion of the input fails
200 public void parseIntoObjectFromBytes(final byte[] content, final String type) throws ObjectTransformationException {
201 try {
202 ByteArrayInputStream bis = new ByteArrayInputStream(content);
203 if (type.equals(Constants.TYPE_NOTE_VNOTE)) {
204 new VNoteParser(bis);
205 noteObject = NoteConverter.convertVNote2Note(VNoteParser.VNote());
206 } else if (type.equals(Constants.TYPE_NOTE_SIFN)) {
207 SIFNParser sn = new SIFNParser(bis);
208 noteObject = sn.parse();
209 } else if (type.equals(Constants.TYPE_NOTE_ICAL)) {
210 CalendarBuilder cbuild = new CalendarBuilder();
211 Calendar cd = cbuild.build(bis);
212 /* Decode Quoted Printables */
213 Calendar decoded = QPDecode.decodeQP(cd);
214 noteObject = NoteConverter.convertical4j2Note(decoded, null);
215 } else if (type.equals(Constants.TYPE_NOTE_PLAIN)) {
216 noteObject = NoteConverter.convertPlain2Note(
217 new String(origItem.getContent()),
218 origItem.getTimestamp().toString());
220 } catch (Exception e) {
221 throw new ObjectTransformationException("Error parsing from type: " + type, e);