2 * MultipleSourceVCardObjectStore.java
4 * Original file created on December 13, 2006, 10:44 PM
5 * Copyright (C) 2006-2010 Mathew McBride
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 package net
.bionicmessage
.objects
;
26 import com
.funambol
.common
.pim
.contact
.Contact
;
27 import com
.funambol
.common
.pim
.contact
.Name
;
28 import com
.funambol
.common
.pim
.vcard
.VcardParser
;
29 import java
.io
.ByteArrayInputStream
;
30 import java
.io
.PrintWriter
;
31 import java
.io
.StringWriter
;
33 import java
.util
.Hashtable
;
34 import java
.util
.List
;
35 import net
.bionicmessage
.groupdav
.GroupDAVObject
;
38 * Storage and synchronization mechanism for vCard objects
41 public class MultipleSourceVCardObjectStore
extends AbstractObjectStore
{
43 Hashtable
<String
, Contact
> objCache
= new Hashtable();
45 public MultipleSourceVCardObjectStore(String storedir
, int options
) {
46 super(storedir
, options
);
49 String
addFromServerToStore(String storeName
, GroupDAVObject obj
, List toAdd
) throws Exception
{
50 Contact ct
= constructContactObject(obj
.getContent());
51 String uid
= ct
.getUid();
52 this.objCache
.put(uid
, ct
);
53 Name name
= ct
.getName();
54 String n
= name
.getDisplayName().getPropertyValueAsString();
55 if (name
.getDisplayName().getPropertyValueAsString() == null) {
56 n
= name
.getFirstName() + " " + name
.getLastName();
59 ContactWrapper cw
= new ContactWrapper(obj
.getContent());
60 this.db
.createObject(storeName
, uid
, obj
.getLocation(), obj
.getEtag(), n
, cw
, null, null);
62 this.objCache
.put(uid
, ct
);
66 public String
addObject(String sourceName
, String uid
, String name
, String contents
)
68 if (contents
.indexOf("\nUID:") == -1) {
69 int endLoc
= contents
.indexOf("END:VCARD");
70 StringBuffer sb
= new StringBuffer();
71 sb
.append(contents
.substring(0, endLoc
));
75 sb
.append(contents
.substring(endLoc
));
76 contents
= sb
.toString();
79 URI sourceURI
= this.hostURI
.resolve((String
) this.sources
.get(sourceName
));
80 URI newURI
= sourceURI
.resolve(uid
+ ".vcf");
82 GroupDAVObject gbo
= this.server
.putObject(newURI
.getPath(), "text/x-vcard", contents
.getBytes());
83 return addFromServerToStore(sourceName
, gbo
.getLocation(), this.addedToServer
);
86 public int replaceObject(String storeName
, String uid
, String name
, String contents
)
88 String
[] urletag
= this.db
.getUrlEtagForUid(uid
);
89 GroupDAVObject gbo
= this.server
.modifyObject(urletag
[0], urletag
[1], "text/x-vcard", contents
.getBytes());
94 this.updatedOnServer
.add(uid
);
95 this.db
.deleteObjectByUid(uid
);
96 addFromServerToStore(storeName
, urletag
[0], this.updatedOnServer
);
100 public int mergeObject(String newuid
, String contents1
, String contents2
)
105 public Contact
getObjectFromStore(String uid
) throws ObjectStoreException
{
106 if (this.objCache
.get(uid
) != null) {
107 this.log
.fine("Object " + uid
+ "has been retrieved from store");
108 Contact ct
= (Contact
) this.objCache
.get(uid
);
112 ContactWrapper cw
= (ContactWrapper
) this.db
.getObjectByUid(uid
);
116 return constructContactObject(cw
.getData());
117 } catch (Exception ex
) {
118 throw new ObjectStoreException(uid
, ObjectStoreException
.OP_DOWNLOAD_ADD
, new byte[0], ex
);
122 private Contact
constructContactObject(byte[] content
)
124 ByteArrayInputStream bis
= new ByteArrayInputStream(content
);
125 VcardParser vcp
= new VcardParser(bis
, null, null);
129 public void printDebugReport() throws Exception
{
130 StringWriter sw
= new StringWriter();
131 PrintWriter lw
= new PrintWriter(sw
);
132 List data
= this.db
.getAllData();
133 for (int i
= 0; i
< data
.size(); ++i
) {
134 Object
[] dataarray
= (Object
[]) data
.get(i
);
135 String uid
= (String
) dataarray
[0];
136 String url
= (String
) dataarray
[1];
138 throw new Exception("URL for " + uid
+ " is null");
140 String source
= (String
) dataarray
[2];
141 String etag
= (String
) dataarray
[3];
142 String name
= (String
) dataarray
[4];
143 ContactWrapper cw
= (ContactWrapper
) dataarray
[7];
144 String objData
= new String(cw
.getData());
145 lw
.println("----------");
146 lw
.println("UID:" + uid
);
147 lw
.println("URL:" + url
);
148 lw
.println("ETAG:" + etag
);
149 lw
.println("NAME:" + name
);
150 lw
.println("DATA FOLLOWS:");
152 lw
.println("----------");
154 lw
.println("Objects added to store: ");
157 for (i
= 0; i
< this.addedToStore
.size(); ++i
) {
158 uid
= (String
) this.addedToStore
.get(i
);
159 lw
.println("A: " + uid
);
161 lw
.println("Objects updated from server: ");
162 for (i
= 0; i
< this.updatedInStore
.size(); ++i
) {
163 uid
= (String
) this.updatedInStore
.get(i
);
164 lw
.println("U: " + uid
);
166 lw
.println("Objects deleted from store: ");
167 for (i
= 0; i
< this.deletedFromStore
.size(); ++i
) {
168 uid
= (String
) this.deletedFromStore
.get(i
);
169 lw
.println("D: " + uid
);
171 lw
.println("Objects added to the server: ");
172 for (i
= 0; i
< this.addedToServer
.size(); ++i
) {
173 uid
= (String
) this.addedToServer
.get(i
);
174 lw
.println("SA: " + uid
);
176 lw
.println("Objects merged to server: ");
177 for (i
= 0; i
< this.updatedOnServer
.size(); ++i
) {
178 uid
= (String
) this.updatedOnServer
.get(i
);
179 lw
.println("SM: " + uid
);
181 lw
.println("Objects deleted from server: ");
182 for (i
= 0; i
< this.deletedFromServer
.size(); ++i
) {
183 uid
= (String
) this.deletedFromServer
.get(i
);
184 lw
.println("SD: " + uid
);
188 this.log
.info(sw
.toString());
191 private int getDownloaderParameter() {
195 void updateObjectFromServer(String uid
, GroupDAVObject obj
) throws Exception
{
196 Contact ct
= constructContactObject(obj
.getContent());
197 Name name
= ct
.getName();
198 this.objCache
.put(uid
, ct
);
199 String nameValue
= name
.getDisplayName().getPropertyValueAsString();
200 if (nameValue
== null) {
201 nameValue
= name
.getFirstName() + " " + name
.getLastName();
203 nameValue
= nameValue
.trim();
204 String strContent
= new String(obj
.getContent());
205 this.db
.updateObject(uid
, obj
.getEtag(), nameValue
, null, null, strContent
);
207 this.updatedInStore
.add(uid
);
210 public String
searchUids(String name
, long dstime
, long detime
) throws Exception
{
211 return this.db
.findUidForNameAndDate(name
, null, null);