Update deps to Funambol 8.7
[funambol-groupdav-connector.git] / src / main / java / net / bionicmessage / funambol / groupdav / contacts / OutboundFunambolContactObject.java
blobf24d12a14366d3d63152f75f6108f9b2e777892c
1 /*
2 * OutboundFunambolContactObject.java
4 * Created on Oct 28, 2007, 1:48:43 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 com.funambol.common.pim.common.Converter;
25 import net.bionicmessage.funambol.framework.OutboundObject;
27 //import com.funambol.foundation.pdi.contact.*;
28 //import com.funambol.foundation.pdi.converter.*;
29 //import com.funambol.foundation.pdi.parser.*;
30 import com.funambol.common.pim.contact.Contact;
31 import com.funambol.common.pim.converter.*;
32 import com.funambol.common.pim.sif.SIFCParser;
33 import java.io.StringReader;
34 import com.funambol.common.pim.vcard.*;
35 import com.funambol.framework.engine.SyncItem;
36 import com.funambol.framework.engine.SyncItemImpl;
37 import com.funambol.framework.engine.source.SyncSource;
38 import java.io.ByteArrayInputStream;
39 import net.bionicmessage.funambol.framework.ObjectTransformationException;
41 /**
43 * @author matt
45 public class OutboundFunambolContactObject implements OutboundObject {
47 public static final String TYPE_SIFC = "text/x-s4j-sifc";
48 public static final String TYPE_VCARD = "text/x-vcard";
49 private Contact conObject;
51 public OutboundFunambolContactObject() {
54 public void setContactObject(Contact obj) {
55 conObject = obj;
58 public Object getPDIDataObject() {
59 return conObject;
62 public Class getPDIDataClass() {
63 return com.funambol.common.pim.contact.Contact.class;
66 public String getStringRepresentation(String type) throws ObjectTransformationException {
67 try {
68 //StringReader sr = new StringReader(type);
69 BaseConverter conv = null;
70 if (type.equals(TYPE_SIFC)) {
71 conv = new ContactToSIFC(null, "UTF-8");
72 } else if (type.endsWith(TYPE_VCARD)) {
73 conv = new ContactToVcard(null, "UTF-8");
75 return conv.convert(conObject);
76 } catch (Exception e) {
77 throw new ObjectTransformationException("Error transforming contact to" + type, e);
81 public void parseIntoObjectFromString(String str, String type) throws ObjectTransformationException {
82 try {
83 ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes("UTF-8"));
84 if (type.equals(TYPE_SIFC)) {
85 SIFCParser scp = new SIFCParser(bis);
86 conObject = scp.parse();
87 } else if (type.equals(TYPE_VCARD)) {
88 VcardParser vcp = new VcardParser(bis);
89 conObject = vcp.vCard();
91 } catch (Exception e) {
92 throw new ObjectTransformationException("Error parsing from type: "+ type, e);
96 public String getDefaultStringOutputType() {
97 return TYPE_VCARD;
100 public SyncItem generateSyncItem(SyncSource origSource) throws Exception {
101 String content = null;
102 String type = origSource.getInfo().getPreferredType().getType();
103 if (type == null) {
104 content = getStringRepresentation(getDefaultStringOutputType());
105 } else {
106 content = getStringRepresentation(type);
108 String uid = conObject.getUid();
109 SyncItemImpl si = new SyncItemImpl(origSource, uid);
110 byte[] con = content.getBytes("UTF-8");
111 si.setContent(con);
112 return si;