KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > spap > AssetSyncSource


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.spap;
20
21 import java.security.Principal JavaDoc;
22 import java.sql.Timestamp JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import sync4j.framework.tools.Base64;
28
29
30 import sync4j.syncclient.common.XMLHashMapParser;
31 import sync4j.syncclient.common.logging.Logger;
32 import sync4j.syncclient.spds.SyncException;
33 import sync4j.syncclient.spds.engine.SyncItem;
34 import sync4j.syncclient.spds.engine.SyncItemImpl;
35 import sync4j.syncclient.spds.engine.SyncItemProperty;
36 import sync4j.syncclient.spds.engine.SyncSource;
37
38
39 /**
40  * This class implements a <i>SyncSource</i> that handle the <code>Asset</code>
41  * store on a device.
42  * <p>Retrieves the list of the Assets and their state using the <code>AssetDAO</code>
43  * that manages the memorization of the Assets using the <i>Device Management</i>
44  * as database.
45  *
46  * <p>This SyncSource maps a Asset into properties of the corresponding SyncItem.
47  * <p>A asset has transformed in XML using the
48  * <code>sync4j.syncclient.framework.provisioning.XMLHashMapParser</code>
49  * class and the xml as gotten is store in the property SyncItem.PROPERTY_BINARY_CONTENT.
50  *
51  * @version $Id: AssetSyncSource.java,v 1.5 2005/01/19 11:18:36 fabius Exp $
52  * @author Stefano Nichele
53  */

54 public class AssetSyncSource implements SyncSource {
55
56
57     // -------------------------------------------------------------- Properties
58

59     private String JavaDoc sourceURI;
60
61     /**
62      * Sets property sourceURI
63      * @param sourceURI
64      * description: uri of the SyncSource
65      * displayName: sourceURI
66      */

67     public void setSourceURI(String JavaDoc sourceURI) {
68         this.sourceURI = sourceURI;
69     }
70
71     /**
72      * Returns property sourceURI
73      */

74     public String JavaDoc getSourceURI() {
75         return sourceURI;
76     }
77
78
79     private String JavaDoc type;
80
81     /**
82      * Sets property type
83      * @param type
84      * description: type of the SyncSource
85      * displayName: type
86      */

87     public void setType(String JavaDoc type) {
88         this.type = type;
89     }
90
91     /**
92      * Returns property type
93      */

94     public String JavaDoc getType() {
95         return type;
96     }
97
98
99     private String JavaDoc name;
100
101     /**
102      * Sets property name
103      * @param name
104      * description: name of the SyncSource
105      * displayName: name
106      */

107     public void setName(String JavaDoc name) {
108         this.name = name;
109     }
110
111     /**
112      * Returns property name
113      */

114     public String JavaDoc getName() {
115         return name;
116     }
117
118
119     // ------------------------------------------------------------ Private data
120

121     /** AssetDAO used for get asset's information */
122     private AssetDAO assetDAO = null;
123
124     private Logger logger = new Logger();
125
126     // ------------------------------------------------------------- Constructor
127

128     /**
129      * Constructs a AssetSyncSource
130      */

131     public AssetSyncSource() {
132         if (logger.isLoggable(Logger.DEBUG)) {
133             logger.debug("Calling AssetSyncSource()");
134         }
135         assetDAO = new AssetDAO();
136     }
137
138
139     /**
140      * Returns a list of the assets except those with state 'D'
141      * @param principal the principal for which the data has to be considered.
142      * @return an array of all SyncItems stored in this source.
143      * If there are no items an empty array is returned.
144      */

145     public SyncItem[] getAllSyncItems(Principal JavaDoc principal) {
146         if (logger.isLoggable(Logger.DEBUG)) {
147             logger.debug("Calling getAllSyncItems in AssetSyncSource");
148         }
149
150         Vector JavaDoc assetsList = assetDAO.getAllAsset();
151         int numAssets = assetsList.size();
152
153         SyncItem[] syncItems = new SyncItem[numAssets];
154
155         SyncItem syncItem = null;
156         String JavaDoc key = null;
157         String JavaDoc type = null;
158         String JavaDoc state = null;
159         String JavaDoc xml = null;
160         Map JavaDoc assetValues = null;
161         Asset asset = null;
162         AssetVersion newVersion = null;
163         AssetVersion currentVersion = null;
164         Timestamp JavaDoc timeStamp = null;
165
166         for (int i=0; i<numAssets; i++) {
167             asset = (Asset)assetsList.elementAt(i);
168             key = asset.getId();
169             state = asset.getState();
170             timeStamp = asset.getLastUpdate();
171
172             if (state.equals(Asset.STATE_DELETE)) {
173                 // skip asset with state='D'
174
continue;
175             }
176
177             syncItem = new SyncItemImpl(this, key, 'N');
178
179             assetValues = asset.toMap();
180             newVersion = asset.getNewVersion();
181             if (newVersion != null) {
182                 assetValues.putAll(newVersion.toMap());
183             } else {
184                 currentVersion = asset.getCurrentVersion();
185                 assetValues.putAll(currentVersion.toMap());
186             }
187
188             syncItem.setProperty(
189                 new SyncItemProperty(
190                     SyncItem.PROPERTY_BINARY_CONTENT ,
191                     Base64.encode((XMLHashMapParser.toXML(assetValues)).getBytes())
192                 )
193             );
194
195             syncItem.setProperty(
196                     new SyncItemProperty(SyncItem.PROPERTY_TIMESTAMP,timeStamp)
197                     );
198
199             syncItems[i] = syncItem;
200         }
201
202         return syncItems;
203     }
204
205     /**
206      *
207      * @param principal
208      * @param since
209      * @return deleted syncItems
210      */

211     public SyncItem[] getDeletedSyncItems(Principal JavaDoc principal, Date JavaDoc since) {
212         if (logger.isLoggable(Logger.DEBUG)) {
213             logger.debug("Calling getDeletedSyncItems in AssetSyncSource");
214         }
215
216         return new SyncItem[0];
217     }
218
219     /**
220      *
221      * @param principal
222      * @param since
223      * @return new syncItems
224      */

225     public SyncItem[] getNewSyncItems(Principal JavaDoc principal, Date JavaDoc since) {
226         if (logger.isLoggable(Logger.DEBUG)) {
227             logger.debug("Calling getNewSyncItems in AssetSyncSource");
228         }
229         return new SyncItem[0];
230     }
231
232     /**
233      *
234      * @param principal
235      * @param since
236      * @return update syncItems
237      */

238     public SyncItem[] getUpdatedSyncItems(Principal JavaDoc principal, Date JavaDoc since) {
239         if (logger.isLoggable(Logger.DEBUG)) {
240             logger.debug("Calling getUpdatedSyncItems in AssetSyncSource");
241         }
242
243         SyncItem[] syncItems = new SyncItem[0];
244         return syncItems;
245     }
246
247
248     /**
249      * Replaces an existing Asset or adds a new Asset if it does not exist.
250      * The contained version is save as <i>newVersion</i>.
251      *
252      * @param principal
253      * @param syncItem
254      * @return syncItem
255      * @throws SyncException
256      */

257     public SyncItem setSyncItem(Principal JavaDoc principal, SyncItem syncItem)
258             throws SyncException{
259
260         if (logger.isLoggable(Logger.DEBUG)) {
261             logger.debug("Calling setSyncItem in AssetSyncSource");
262         }
263
264         String JavaDoc key = syncItem.getKey().getKeyAsString();
265
266         Date JavaDoc dateSync = (Date JavaDoc)syncItem.getPropertyValue(SyncItem.PROPERTY_TIMESTAMP);
267
268
269         Timestamp JavaDoc timeSync = new Timestamp JavaDoc(dateSync.getTime());
270
271         String JavaDoc xml = new String JavaDoc(
272             Base64.decode(
273                 (byte[])syncItem.getPropertyValue(SyncItem.PROPERTY_BINARY_CONTENT)
274             )
275         );
276
277         // transforms the SyncItem.PROPERTY_BINARY_CONTENT in Map
278
Map JavaDoc values = XMLHashMapParser.toMap(xml);
279
280
281         // Cheks if a asset with the same key already exists and if has a currentVersion
282
Asset asset = null;
283         AssetVersion currentVersion = null;
284         try {
285             asset = assetDAO.getAsset(key);
286             currentVersion = asset.getCurrentVersion();
287         }
288         catch (AssetManagementException ex) {
289         }
290
291         // creates a new asset with a newVersion
292
asset = new Asset(values, true);
293         asset.setId(key);
294
295         // if a asset with the same key already exists set the currentVersion
296
// of the new asset and set the state to 'U'
297
if (currentVersion != null) {
298             asset.setCurrentVersion(currentVersion);
299             asset.setState(Asset.STATE_UPDATE);
300         } else {
301             asset.setState(Asset.STATE_NEW);
302         }
303
304         asset.setLastUpdate(timeSync);
305
306         try {
307             assetDAO.setAsset(asset, timeSync);
308         }
309         catch (AssetManagementException ex) {
310             throw new SyncException("Error setting item: " + key, ex);
311         }
312
313         return syncItem;
314
315     }
316
317     /**
318      * Removes a Asset given its key.
319      *
320      * @param principal the entity that wants to do the operation
321      * @param syncItem the item to remove
322      * @throws SyncException in case of error
323      */

324     public void removeSyncItem(Principal JavaDoc principal, SyncItem syncItem)
325             throws SyncException {
326
327         if (logger.isLoggable(Logger.DEBUG)) {
328             logger.debug("Calling removeSyncItem in AssetSyncSource");
329         }
330
331         String JavaDoc key = syncItem.getKey().getKeyAsString();
332
333         Date JavaDoc dateSync = (Date JavaDoc)syncItem.getPropertyValue(SyncItem.PROPERTY_TIMESTAMP);
334
335         try {
336             assetDAO.setAssetState(key, "D");
337
338             /**
339              * @todo
340              */

341             // for a bug of the SyncPlatform the dateSync is null
342
//assetDAO.setAssetLastUpdate(key, "" + dateSync.getTime());
343
}
344         catch (AssetManagementException ex) {
345             throw new SyncException("Error remove item '" + key + "'", ex);
346         }
347     }
348
349
350     public void beginSync(int syncMode) {
351
352     }
353
354     public void beginSync() {
355
356     }
357
358     public void commitSync() {
359
360     }
361 }
Popular Tags