KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayOutputStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.sql.Timestamp JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Vector JavaDoc;
26
27
28
29 import sync4j.syncclient.common.HashtableTools;
30 import sync4j.syncclient.spdm.DMException;
31 import sync4j.syncclient.spdm.DeviceManager;
32 import sync4j.syncclient.spdm.ManagementNode;
33 import sync4j.syncclient.spdm.SimpleDeviceManager;
34
35
36 /**
37  * This class supplies the methods for use the DM like repository of the
38  * information on the present assets on the client.
39  *
40  * The DM is used with the following node structure:
41  *
42  * <PRE>
43  * conduit
44  * applications
45  * &#60;manufacturer&#62;
46  * &#60;asset's name&#62;
47  * asset (contains the information
48  * of the asset)
49  * currentVersion (contains the information
50  * of the version installed)
51  * newVersion (contains the information
52  * of the new version to install)
53  *</PRE>
54  *
55  * @version $Id: AssetDAO.java,v 1.2 2005/01/19 11:18:36 fabius Exp $
56  * @author Stefano Nichele
57  */

58 public class AssetDAO {
59
60     // ---------------------------------------------------------------Constants
61

62     /** Name of the DM node used for read/write the asset's information */
63     private static final String JavaDoc CONFIG_ASSET_NODE_NAME = "asset";
64
65     /** Name of the DM node used for read/write the current version's information */
66     private static final String JavaDoc CONFIG_CURRENT_VERSION_NODE_NAME = "currentVersion";
67
68     /** Name of the DM node used for read/write the new version's information */
69     private static final String JavaDoc CONFIG_NEW_VERSION_NODE_NAME = "newVersion";
70
71     /** Name of the DM node used for read/write the applications' information */
72     private static final String JavaDoc CONFIG_MANAGEMENT_NODE_NAME = "spap/applications";
73
74
75
76
77     // ------------------------------------------------------------Private data
78

79     /** Management node for access to the DM */
80     private ManagementNode managementNode = null;
81
82
83
84     // -------------------------------------------------------------Constructor
85
/**
86      * Constructs a new AssetDAO
87      */

88     public AssetDAO() {
89         DeviceManager dm = SimpleDeviceManager.getDeviceManager();
90         managementNode = dm.getManagementTree(CONFIG_MANAGEMENT_NODE_NAME);
91     }
92
93     /**
94      * Save the asset.
95      * The asset is saved in DM.
96      *
97      * @param asset asset to save
98      * @param time time of the saving
99      * @throws AssetManagementException if an error occurs during the saving
100      */

101     public void setAsset(Asset asset, Timestamp JavaDoc time) throws AssetManagementException {
102
103         String JavaDoc manufacturer = asset.getManufacturer().toLowerCase();
104         String JavaDoc name = asset.getName().toLowerCase();
105
106         String JavaDoc state = asset.getState();
107         String JavaDoc id = asset.getId();
108         String JavaDoc description = null2empty(asset.getDescription());
109         String JavaDoc lastUpdate = "" + time.getTime();
110
111         AssetVersion currentVersion = asset.getCurrentVersion();
112         AssetVersion newVersion = asset.getNewVersion();
113
114
115         String JavaDoc assetNodePath = manufacturer + "/" + name + "/" + CONFIG_ASSET_NODE_NAME;
116         try {
117
118             // save the asset
119
managementNode.setValue(assetNodePath, Asset.PROPERTY_ID ,id);
120             managementNode.setValue(assetNodePath, Asset.PROPERTY_STATE ,state);
121             managementNode.setValue(assetNodePath, Asset.PROPERTY_NAME ,asset.getName());
122             managementNode.setValue(assetNodePath, Asset.PROPERTY_DESCRIPTION , description);
123             managementNode.setValue(assetNodePath, Asset.PROPERTY_MANUFACTURER , asset.getManufacturer());
124             managementNode.setValue(assetNodePath, Asset.PROPERTY_LAST_UPDATE, lastUpdate);
125
126             // save the current version
127
if (currentVersion != null) {
128                 setAssetVersion(manufacturer, name, currentVersion, false);
129             } else {
130                 managementNode.removeNode(manufacturer + "/" + name + "/" + CONFIG_CURRENT_VERSION_NODE_NAME);
131             }
132
133             // save the new version
134
if (newVersion != null) {
135                 setAssetVersion(manufacturer, name, newVersion, true);
136             } else {
137                 managementNode.removeNode(manufacturer + "/" + name + "/" + CONFIG_NEW_VERSION_NODE_NAME);
138             }
139
140         }
141         catch (DMException ex) {
142             throw new AssetManagementException(asset, "Error setting asset", ex);
143         }
144
145     }
146
147     /**
148      * Returns the asset with the given identifier
149      *
150      * @param idAsset the identifier of the asset
151      * @return the Asset with the given identifier
152      * @throws AssetManagementException if Asset not exist
153      */

154     public Asset getAsset(String JavaDoc idAsset) throws AssetManagementException {
155         Asset asset = null;
156
157         ManagementNode[] manufacturesNode = null;
158
159         try {
160             manufacturesNode = managementNode.getChildren();
161         }
162         catch (DMException ex) {
163             throw new AssetManagementException("Asset with id: '" +
164                                      idAsset + "' not found", ex);
165         }
166
167         int numManufacturer = manufacturesNode.length;
168
169         ManagementNode manufacturerNode = null;
170         ManagementNode[] assetsNode = null;
171         ManagementNode assetNode = null;
172
173         boolean assetFound = false;
174         Hashtable JavaDoc currentVersion = null;
175         Hashtable JavaDoc newVersion = null;
176
177         int numAsset = 0;
178         for (int i=0; i<numManufacturer; i++) {
179             manufacturerNode = manufacturesNode[i];
180             try {
181                 assetsNode = manufacturerNode.getChildren();
182             }
183             catch (DMException ex) {
184                 continue;
185             }
186
187             numAsset = assetsNode.length;
188             for (int j=0; j<numAsset; j++) {
189                 assetNode = assetsNode[j];
190
191                 String JavaDoc id = null;
192
193                 try {
194
195                     id = (String JavaDoc)assetNode.getNodeValue(
196                             CONFIG_ASSET_NODE_NAME, Asset.PROPERTY_ID
197                             );
198
199                     if (idAsset.equalsIgnoreCase(id)) {
200
201                         // load asset
202
asset = new Asset(
203                                 HashtableTools.hashtable2hashMap(
204                                  assetNode.getNodeValues(CONFIG_ASSET_NODE_NAME)
205                                 )
206                                 );
207
208                         assetFound = true;
209
210                         // load currentVersion
211
try {
212                             currentVersion = assetNode.getNodeValues(CONFIG_CURRENT_VERSION_NODE_NAME);
213                             asset.setCurrentVersion(HashtableTools.hashtable2hashMap(currentVersion));
214                         }
215                         catch (DMException ex) {
216                             // ignore this exception because the asset could not have the currentVersion
217
}
218
219                         // load newVersion
220
try {
221                             newVersion = assetNode.getNodeValues(CONFIG_NEW_VERSION_NODE_NAME);
222                             asset.setNewVersion(HashtableTools.hashtable2hashMap(newVersion));
223                         }
224                         catch (DMException ex) {
225                             // ignore this exception because the asset could not have the newVersion
226
}
227
228                         // interrupted search in the list of this manufacturer
229
break;
230
231                     }
232
233                 }
234                 catch (DMException ex) {
235                     // ignore this exception
236
continue;
237                 }
238
239             }
240
241             if (assetFound) {
242                 // interrupted search
243
break;
244             }
245         }
246
247         if (!assetFound) {
248             throw new AssetManagementException("Asset with id: '" +
249                                      idAsset + "' not found");
250         }
251
252         return asset;
253     }
254
255
256     /**
257      * Sets the state of the asset identified from the given idAsset
258      *
259      * @param idAsset identifier of the asset of which wants to set the state
260      * @param state state of the asset
261      * @return asset with the changed state
262      * @throws AssetManagementException if an error occurs or if asset not exists
263      */

264     public Asset setAssetState(String JavaDoc idAsset, String JavaDoc state)
265             throws AssetManagementException {
266         Asset asset = getAsset(idAsset);
267         return setAssetState(asset,state);
268     }
269
270     /**
271      * Sets the state of the asset.
272      *
273      * @param asset asset of which wants to set the state
274      * @param state state of the asset
275      * @return asset with the changed state
276      * @throws AssetManagementException if a error occurs during setting
277      */

278     public Asset setAssetState(Asset asset, String JavaDoc state) throws AssetManagementException {
279         String JavaDoc manufacturer = asset.getManufacturer().toLowerCase();
280         String JavaDoc assetName = asset.getName().toLowerCase();
281
282         ManagementNode assetNode = null;
283
284         java.util.Date JavaDoc time = new java.util.Date JavaDoc();
285
286         try {
287             assetNode = managementNode.getChildNode(manufacturer + "/"
288                     + assetName);
289
290             assetNode.setValue(CONFIG_ASSET_NODE_NAME, Asset.PROPERTY_STATE , state);
291         }
292         catch (DMException e) {
293             e.printStackTrace();
294             return null;
295         }
296
297         asset.setState(state);
298
299         return asset;
300     }
301
302     /**
303      * Returns the list of all asset
304      *
305      * @return the list of all asset
306      */

307     public Vector JavaDoc getAllAsset() {
308         return listAsset(null);
309     }
310
311     /**
312      * Returns the state of the asset identified from the given id
313      * @param idAsset identifier of the asset
314      *
315      * @return the state of the asset
316      * @throws AssetManagementException if asset not exists
317      */

318     public String JavaDoc getAssetState(String JavaDoc idAsset) throws AssetManagementException {
319
320         Asset asset = getAsset(idAsset);
321
322         return asset.getState();
323     }
324
325
326
327     /**
328      * Returns the list of the asset with the given state.
329      * If the given <code>state</code> is null, returns
330      * the list of the all asset.
331      *
332      * @param state the state of the wanted assets.
333      * @return the list of the asset
334      */

335     public Vector JavaDoc listAsset(String JavaDoc state) {
336         Vector JavaDoc assets = new Vector JavaDoc();
337
338         ManagementNode[] manufacturesNode = null;
339
340         try {
341             manufacturesNode = managementNode.getChildren();
342         }
343         catch (DMException ex) {
344             // if an error occurs returns an empty list
345
return assets;
346         }
347
348         int numManufacturer = manufacturesNode.length;
349
350         ManagementNode manufacturerNode = null;
351         ManagementNode[] assetsNode = null;
352         ManagementNode assetNode = null;
353         String JavaDoc assetName = null;
354         Hashtable JavaDoc currentVersion = null;
355         Hashtable JavaDoc newVersion = null;
356         AssetVersion currentVersionAsset = null;
357         AssetVersion newVersionAsset = null;
358         Asset asset = null;
359         String JavaDoc idAsset = null;
360         boolean foundNewVersion = false;
361         boolean foundCurrentVersion = false;
362         String JavaDoc assetState = null;
363         boolean assetFound = false;
364
365         int numAsset = 0;
366
367         // for all manufacturer
368

369         for (int i=0; i<numManufacturer; i++) {
370             manufacturerNode = manufacturesNode[i];
371             try {
372                 assetsNode = manufacturerNode.getChildren();
373             }
374             catch (DMException ex) {
375                 continue;
376             }
377             numAsset = assetsNode.length;
378
379
380             // for all asset of this manifacturer
381

382             for (int j=0; j<numAsset; j++) {
383                 assetFound = false;
384                 asset = null;
385
386                 assetNode = assetsNode[j];
387
388                 try {
389                     asset = new Asset(
390                               HashtableTools.hashtable2hashMap(
391                                 assetNode.getNodeValues(CONFIG_ASSET_NODE_NAME)
392                               )
393                             );
394                     currentVersion = null;
395                     newVersion = null;
396                 }
397                 catch (DMException ex) {
398                     // ignore this exception
399
}
400
401                 // if asset node not exists then asset==null
402
if (asset == null) {
403                     continue;
404                 }
405
406                 assetState = asset.getState();
407
408                 if (state != null) {
409                     if (state.equals(assetState)) {
410                         assetFound = true;
411                     }
412                 } else {
413                     // if stateWanted==null, returns all assets
414
assetFound = true;
415                 }
416
417
418                 if (assetFound) {
419                     try {
420                         currentVersion = assetNode.getNodeValues(
421                                 CONFIG_CURRENT_VERSION_NODE_NAME
422                                 );
423                     }
424                     catch (DMException ex) {
425                         // ignore this exception because the asset could not have the currentVersion
426
}
427
428                     if (currentVersion != null) {
429                         asset.setCurrentVersion(
430                                 new AssetVersion(
431                                   HashtableTools.hashtable2hashMap(currentVersion)
432                                 )
433                                 );
434                     }
435
436                     try {
437                         newVersion = assetNode.getNodeValues(CONFIG_NEW_VERSION_NODE_NAME);
438                     }
439                     catch (DMException ex) {
440                         // ignore this exception because the asset could not have the newVersion
441
}
442
443                     if (newVersion != null) {
444                         asset.setNewVersion(
445                                 new AssetVersion(HashtableTools.hashtable2hashMap(newVersion))
446                                 );
447                     }
448
449                     assets.addElement(asset);
450                 }
451             }
452         }
453         return assets;
454     }
455
456
457     /**
458      * Remove the asset from DM
459      *
460      * @param asset asset to remove
461      * @throws AssetManagementException if an error occurs
462      */

463     public void removeAsset(Asset asset) throws AssetManagementException {
464         String JavaDoc manufacturer = asset.getManufacturer().toLowerCase();
465         String JavaDoc name = asset.getName().toLowerCase();
466
467         try {
468             managementNode.removeNode(manufacturer + "/" + name);
469
470             // if there are not other asset for this manufacturer,
471
// remove the manufacturer node
472
ManagementNode manufacturerNode =
473                     managementNode.getChildNode(manufacturer);
474
475             if (manufacturerNode.getChildren().length == 0) {
476                 managementNode.removeNode(manufacturer);
477             }
478         }
479         catch (DMException ex) {
480             throw new AssetManagementException(
481                     asset, "Error removing asset information from DM", ex
482                     );
483         }
484     }
485
486     /**
487      * Sets the asset as NOT VALID
488      *
489      * @param asset asset to set as NOT VALID
490      * @param cause the cause for which the asset is not valid
491      * @return asset asset with the state changed
492      * @throws AssetManagementException if a error occurs
493      */

494     public Asset setAssetAsNotValid(Asset asset, Throwable JavaDoc cause)
495             throws AssetManagementException {
496
497         String JavaDoc manufacturer = asset.getManufacturer().toLowerCase();
498         String JavaDoc assetName = asset.getName().toLowerCase();
499
500         ManagementNode assetNode = null;
501
502         String JavaDoc causeMessage = stackTraceToString(cause);
503
504         try {
505             assetNode = managementNode.getChildNode(manufacturer + "/" + assetName);
506         }
507         catch (DMException ex) {
508             throw new AssetManagementException(
509                     asset, "Error during the setting state as NOT VALID", ex
510                     );
511         }
512
513         try {
514             assetNode.setValue(CONFIG_ASSET_NODE_NAME, Asset.PROPERTY_STATE ,
515                                Asset.STATE_NOT_VALID);
516
517             assetNode.setValue(CONFIG_ASSET_NODE_NAME, "CAUSE" , causeMessage);
518         }
519         catch (DMException ex) {
520             throw new AssetManagementException(
521                     asset, "Error during the setting state as NOT VALID", ex
522                     );
523         }
524
525         asset.setState(Asset.STATE_NOT_VALID);
526
527         return asset;
528     }
529
530
531     /**
532      * Transforms a null object in an empty string.
533      * If the object is not null, returns a string representation of the object.
534      *
535      * @param temp object to transform
536      * @return a string representation of the object
537      */

538     private String JavaDoc null2empty(Object JavaDoc temp) {
539         if (temp==null) {
540             return "";
541         } else {
542             return temp.toString();
543         }
544     }
545
546
547     /**
548      * Saving the given AssetVersion for the asset identified from the specified
549      * manufacturer and name.
550      * <p>If <i>isNewVersion</i> is <code>true</code>, the given version is
551      * saved as <i>newVersion</i> otherwise is saved as <i>currentVersion</i>
552      *
553      * @param manufacturer manufacturer of the asset to modify
554      * @param name name of the asset to modify
555      * @param assetVersion version to saving
556      * @param isNewVersion if <code>true</code>, save
557      * the version as <i>newVersion</i>,
558      * otherwise save the version as <i>currentVersion</i>
559      * @throws DMException if an error occurs
560      */

561     protected void setAssetVersion(String JavaDoc manufacturer, String JavaDoc name,
562                                    AssetVersion assetVersion, boolean isNewVersion)
563             throws DMException {
564
565         String JavaDoc nodePath = null;
566
567         if (isNewVersion) {
568             nodePath = manufacturer + "/" + name + "/" +
569                        CONFIG_NEW_VERSION_NODE_NAME;
570         } else {
571             nodePath = manufacturer + "/" + name + "/" +
572                        CONFIG_CURRENT_VERSION_NODE_NAME;
573         }
574
575         managementNode.setValue(nodePath, AssetVersion.PROPERTY_VERSION, assetVersion.getVersion());
576         managementNode.setValue(nodePath, AssetVersion.PROPERTY_RELEASE_DATE , assetVersion.getReleaseDate());
577         managementNode.setValue(nodePath, AssetVersion.PROPERTY_RELEASE_NOTES , null2empty(assetVersion.getReleaseNotes()));
578         managementNode.setValue(nodePath, AssetVersion.PROPERTY_URL, assetVersion.getUrl().toString());
579         managementNode.setValue(nodePath, AssetVersion.PROPERTY_SIZE_ASSET_FILE, ""+assetVersion.getSizeContentFile());
580         managementNode.setValue(nodePath, AssetVersion.PROPERTY_INSTALL_PROGRAM , null2empty(assetVersion.getInstallProgram()));
581         managementNode.setValue(nodePath, AssetVersion.PROPERTY_UNINSTALL_PROGRAM , null2empty(assetVersion.getUninstallProgram()));
582         managementNode.setValue(nodePath, AssetVersion.PROPERTY_NEED_UNINSTALL_PREV , null2empty(assetVersion.getNeedUninstallPrev()));
583
584     }
585
586     /**
587      * Print the exception's stackTrace into a String
588      *
589      * @param e the exception
590      * @return stackTrace
591      */

592     private static String JavaDoc stackTraceToString(Throwable JavaDoc e){
593         String JavaDoc stackTraceString = "";
594         ByteArrayOutputStream JavaDoc sBuf = new ByteArrayOutputStream JavaDoc( 1024 );
595         PrintWriter JavaDoc s = new PrintWriter JavaDoc ( sBuf );
596         e.printStackTrace( s );
597         s.flush();
598         stackTraceString = sBuf.toString();
599         s.close();
600         return stackTraceString;
601     }
602
603 }
Popular Tags