2 * InboundFunambolContactObject.java
4 * Created on Oct 28, 2007, 1:14:16 PM
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
;
41 * Represents a Contact object in bound to ContactSyncSource
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
) {
61 public ContactSyncSource
getSyncSource() {
65 public void setOriginalSyncItem(SyncItem si
) throws Exception
{
67 byte[] content
= si
.getContent();
68 parseIntoObjectFromBytes(content
, si
.getType());
69 setUid(si
.getKey().getKeyAsString());
72 public SyncItem
getOriginalSyncItem() throws Exception
{
76 public void setUid(String uid
) {
78 contactObject
.setUid(uid
);
81 public String
getUid() {
85 public void setDestinationStore(String destination
) {
86 destinationStore
= destination
;
89 public String
getDestinationStore() {
90 return destinationStore
;
93 public Object
getPDIDataObject() {
97 public Class
getPDIDataClass() {
101 public String
getStringRepresentation(String type
) throws ObjectTransformationException
{
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
);
108 } else if (type
.equals(Constants
.TYPE_CONTACT_SIFC
)) {
109 ContactToXML ctos
= new ContactToXML(null, ContactToVcard
.CHARSET_UTF8
);
110 String str
= ctos
.convert(contactObject
);
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
{
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
{
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();