-> bionicmessage groupware sync r1
[funambol-groupdav-connector.git] / src / net / bionicmessage / funambol / groupdav / contacts / InboundFunambolContactObject.java
bloba00c9e53757501ff3da9678de20450d2eeaf8618
1 /*
2 * InboundFunambolContactObject.java
3 *
4 * Created on Oct 28, 2007, 1:14:16 PM
5 *
6 * GroupDAV connector for Funambol v6.5
7 * Copyright (C) 2007 Mathew McBride
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package net.bionicmessage.funambol.groupdav.contacts;
24 import net.bionicmessage.funambol.framework.InboundObject;
25 //import com.funambol.common.pim.contact.*;
26 //import com.funambol.common.pim.converter.ContactToSIFC;
27 //import com.funambol.common.pim.converter.ContactToVcard;
28 //import com.funambol.common.pim.sif.SIFCParser;
29 //import com.funambol.common.pim.vcard.VcardParser;
30 import com.funambol.foundation.pdi.contact.*;
31 import com.funambol.foundation.pdi.converter.*;
32 import com.funambol.foundation.pdi.parser.*;
33 import com.funambol.framework.engine.SyncItem;
34 import com.funambol.framework.server.ConvertDatePolicy;
35 import java.io.ByteArrayInputStream;
36 import java.io.UnsupportedEncodingException;
37 import java.util.TimeZone;
38 import net.bionicmessage.funambol.framework.Constants;
39 import net.bionicmessage.funambol.framework.ObjectTransformationException;
40 /**
41 * Represents a Contact object in bound to ContactSyncSource
42 * @author matt
45 public class InboundFunambolContactObject implements InboundObject {
47 private String destinationStore = "default";
48 private Contact contactObject = null;
49 private ContactSyncSource css = null;
50 private String uid = null;
51 private SyncItem origItem;
53 public InboundFunambolContactObject() {
57 public void setSyncSource(ContactSyncSource ss) {
58 css = ss;
61 public ContactSyncSource getSyncSource() {
62 return css;
65 public void setOriginalSyncItem(SyncItem si) throws Exception {
66 origItem = si;
67 byte[] content = si.getContent();
68 parseIntoObjectFromBytes(content, si.getType());
69 setUid(si.getKey().getKeyAsString());
72 public SyncItem getOriginalSyncItem() throws Exception {
73 return origItem;
76 public void setUid(String uid) {
77 this.uid = uid;
78 contactObject.setUid(uid);
81 public String getUid() {
82 return uid;
85 public void setDestinationStore(String destination) {
86 destinationStore = destination;
89 public String getDestinationStore() {
90 return destinationStore;
93 public Object getPDIDataObject() {
94 return contactObject;
97 public Class getPDIDataClass() {
98 return Contact.class;
101 public String getStringRepresentation(String type) throws ObjectTransformationException {
102 try {
103 if (type.equals(Constants.TYPE_CONTACT_VCARD21) ||
104 type.equals(Constants.TYPE_CONTACT_VCARD30)) {
105 ContactToVcard ctov = new ContactToVcard(null, ContactToVcard.CHARSET_UTF8);
106 String str = ctov.convert(contactObject);
107 return str;
108 } else if (type.equals(Constants.TYPE_CONTACT_SIFC)) {
109 ContactToXML ctos = new ContactToXML(null, ContactToVcard.CHARSET_UTF8);
110 String str = ctos.convert(contactObject);
111 return str;
113 } catch (Exception e) {
114 throw new ObjectTransformationException("Error parsing contact from " + type, e);
116 throw new UnsupportedOperationException("Type: " + type + " unsupported");
119 public void parseIntoObjectFromString(String str, String type) throws ObjectTransformationException {
120 try {
121 parseIntoObjectFromBytes(str.getBytes("UTF-8"), type);
122 } catch (UnsupportedEncodingException e) {
123 throw new ObjectTransformationException("Error outputting content in UTF-8");
127 public void parseIntoObjectFromBytes(byte[] content, String type) throws ObjectTransformationException {
128 try {
129 ByteArrayInputStream bis = new ByteArrayInputStream(content);
130 if (type.equals(Constants.TYPE_CONTACT_VCARD21) || type.equals(Constants.TYPE_CONTACT_VCARD30)) {
131 VcardParser vcp = new VcardParser(bis, null, null);
132 contactObject = vcp.vCard();
133 } else if (type.equals(Constants.TYPE_CONTACT_SIFC)) {
134 XMLContactParser sc = new XMLContactParser(bis);
135 contactObject = sc.parse();
137 } catch (Exception e) {
138 throw new ObjectTransformationException("Error parsing from type: " + type, e);
142 public String getDefaultStringOutputType() {
143 return Constants.TYPE_CONTACT_VCARD21;
146 public String getObjectName() {
147 return contactObject.getName().getDisplayName().getPropertyValueAsString();