KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Timestamp JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25
26
27 /**
28  * This class represents a asset.
29  * <p>A <code>Asset</code> is constituted from:
30  * <ul>
31  * <li>a state: the JNDI name of the datasource to be useed
32  * <li>an identifier: the name of the table containg the items
33  * <li>a name: the column name of the primary key
34  * <li>a manufacturer: the column name of the timestamp
35  * <li>a description: the column name of the change column
36  * <li>a a time of the last update: the column name of the primary key
37  * <li>a current version: is the <code>AssetVersion</code>
38  * for the version installed
39  * <li>a new version: is the <code>AssetVersion</code>
40  * for the version to install
41  * </ul>
42  *
43  * @version $Id: Asset.java,v 1.3 2005/01/19 11:18:36 fabius Exp $
44  * @author Stefano Nichele
45  */

46 public class Asset {
47
48     // --------------------------------------------------------------- Constants
49

50     // These are the possible state of the Asset
51
public static final String JavaDoc STATE_NEW = "N";
52     public static final String JavaDoc STATE_UPDATE = "U";
53     public static final String JavaDoc STATE_DELETE = "D";
54     public static final String JavaDoc STATE_ASSET_INSTALLED = "AI";
55     public static final String JavaDoc STATE_FILE_DOWNLOAD = "FD";
56     public static final String JavaDoc STATE_FILE_EXTRACTED = "FE";
57     public static final String JavaDoc STATE_PREVIOUS_VERSION_UNINSTALLED = "PVU";
58     public static final String JavaDoc STATE_NEW_VERSION_NOT_WANTED = "NVNW";
59     public static final String JavaDoc STATE_NOT_VALID = "NV";
60
61
62     // The following constants are used for create a Map from a Asset
63
// and for to represent a Asset in Map
64
public static final String JavaDoc PROPERTY_ID = "id";
65     public static final String JavaDoc PROPERTY_NAME = "name";
66     public static final String JavaDoc PROPERTY_MANUFACTURER = "manufacturer";
67     public static final String JavaDoc PROPERTY_DESCRIPTION = "description";
68     public static final String JavaDoc PROPERTY_STATE = "state";
69     public static final String JavaDoc PROPERTY_LAST_UPDATE = "last_update";
70
71
72     // -------------------------------------------------------------- Properties
73

74     /**
75      * Identifier of the asset
76      */

77     private String JavaDoc id;
78
79     /**
80      * Sets property id
81      * @param id
82      * description: identifier of the asset
83      * displayName: id
84      */

85     public void setId(String JavaDoc id) {
86         this.id = id;
87     }
88
89     /**
90      * Returns property id
91      */

92     public String JavaDoc getId() {
93         return id;
94     }
95
96     /**
97      * Manufacturer or developer of the asset
98      */

99     private String JavaDoc manufacturer;
100
101     /**
102      * Sets property
103      * @param manufacturer
104      * description: manufacturer or developer of the asset
105      * displayName: manufacturer
106      */

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

114     public String JavaDoc getManufacturer() {
115         return manufacturer;
116     }
117
118
119     /**
120      * Name of the asset
121      */

122     private String JavaDoc name;
123
124     /**
125      * Sets property
126      * @param name
127      * description: name of the asset
128      * displayName: name
129      */

130     public void setName(String JavaDoc name) {
131         this.name = name;
132     }
133
134     /**
135      * Returns property name
136      */

137     public String JavaDoc getName() {
138         return name;
139     }
140
141
142     /**
143      * Description of the asset
144      */

145     private String JavaDoc description;
146
147     /**
148      * Sets property
149      * @param description
150      * description: description of the asset
151      * displayName: description
152      */

153     public void setDescription(String JavaDoc description) {
154         this.description = description;
155     }
156
157     /**
158      * Returns property description
159      */

160     public String JavaDoc getDescription() {
161         return description;
162     }
163
164     /**
165      * State of the asset
166      */

167     private String JavaDoc state;
168
169     /**
170      * Sets property
171      * @param state
172      * description: state of the asset
173      * displayName: state
174      */

175     public void setState(String JavaDoc state) {
176         this.state = state;
177     }
178
179     /**
180      * Returns property state
181      */

182     public String JavaDoc getState() {
183         return state;
184     }
185
186     /**
187      * Time of the last update of the asset
188      */

189     private Timestamp JavaDoc lastUpdate;
190
191     /**
192      * Sets property
193      * @param lastUpdate
194      * description: time of the last update of the asset
195      * displayName: lastUpdate
196      */

197     public void setLastUpdate(Timestamp JavaDoc lastUpdate) {
198         this.lastUpdate = lastUpdate;
199     }
200
201     /**
202      * Returns property lastUpdate
203      */

204     public Timestamp JavaDoc getLastUpdate() {
205         return lastUpdate;
206     }
207
208
209     /**
210      * Current version of the asset
211      */

212     private AssetVersion currentVersion;
213
214     /**
215      * Sets property
216      * @param currentVersion
217      * description: current version of the asset
218      * displayName: currentVersion
219      */

220     public void setCurrentVersion(AssetVersion currentVersion) {
221         this.currentVersion = currentVersion;
222     }
223
224     /**
225      * Returns property currentVersion
226      */

227     public AssetVersion getCurrentVersion() {
228         return currentVersion;
229     }
230
231
232     /**
233      * New version of the asset
234      */

235     private AssetVersion newVersion;
236
237     /**
238      * Sets property
239      * @param newVersion
240      * description: new version of the asset
241      * displayName: newVersion
242      */

243     public void setNewVersion(AssetVersion newVersion) {
244         this.newVersion = newVersion;
245     }
246
247     /**
248      * Returns property newVersion
249      */

250     public AssetVersion getNewVersion() {
251         return newVersion;
252     }
253
254
255     // ------------------------------------------------------------ Constructors
256

257     /**
258      * Constructs a Asset with the values contained in the given Map
259      */

260     public Asset(Map JavaDoc values) {
261         this.id = (String JavaDoc)values.get(PROPERTY_ID);
262         this.manufacturer = (String JavaDoc)values.get(PROPERTY_MANUFACTURER);
263         this.name = (String JavaDoc)values.get(PROPERTY_NAME);
264         this.description = (String JavaDoc)values.get(PROPERTY_DESCRIPTION);
265         this.state = (String JavaDoc)values.get(PROPERTY_STATE);
266
267         //
268
// lastUpdate can be specified as a date in the format YYYY-MM-DD HH:MM:SS.S
269
// or millisecond since Jan the 1st 1970
270
//
271
String JavaDoc lastUpdate = (String JavaDoc)values.get(PROPERTY_LAST_UPDATE);
272         if (lastUpdate != null) {
273             try {
274                 SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss.S");
275                 this.lastUpdate = new Timestamp JavaDoc(format.parse(lastUpdate).getTime());
276             } catch (Exception JavaDoc e1) {
277                 try {
278                     this.lastUpdate = new Timestamp JavaDoc(Long.parseLong(lastUpdate));
279                 } catch (Exception JavaDoc e2) {
280                     throw new IllegalArgumentException JavaDoc(
281                         "'" +
282                         lastUpdate +
283                         "' is neither a date (yyyy-MM-dd HH:mm:ss.S) " +
284                         "nor a timestamp in milliseconds."
285                      );
286                  }
287
288             }
289         }
290     }
291
292     /**
293      * Constructs a Asset with the values contained in the given Map.
294      * <p>If the param <code>buildNewVersion</code> is true it comes
295      * built also the new version
296      * using the same given Map
297      */

298     public Asset(Map JavaDoc values, boolean buildNewVersion) {
299         this(values);
300
301         if (buildNewVersion) {
302             newVersion = new AssetVersion(values);
303         }
304     }
305
306     // ---------------------------------------------------------- Public methods
307

308     /**
309      * Sets the current version using the given Map
310      * @param values the Map used for build a <code>AssetVersion</code>
311      */

312     public void setCurrentVersion(Map JavaDoc values) {
313         currentVersion = new AssetVersion(values);
314     }
315
316     /**
317      * Sets the new version using the given Map
318      * @param values the Map used for build a <code>AssetVersion</code>
319      */

320     public void setNewVersion(Map JavaDoc values) {
321         newVersion = new AssetVersion(values);
322     }
323
324     /**
325      * Returns a representation of a Asset in Map
326      * @return the Map representation
327      */

328     public Map JavaDoc toMap() {
329         Map JavaDoc map = new HashMap JavaDoc();
330
331         map.put(PROPERTY_ID, getId());
332         map.put(PROPERTY_NAME,getName());
333         map.put(PROPERTY_DESCRIPTION,getDescription());
334         map.put(PROPERTY_MANUFACTURER,getManufacturer());
335         map.put(PROPERTY_STATE, getState());
336         map.put(PROPERTY_LAST_UPDATE, getLastUpdate());
337
338         return map;
339     }
340
341
342     //---------------------------------------------------
343
// Overrides java.lang.Object method
344
//---------------------------------------------------
345

346     public String JavaDoc toString() {
347         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Asset: \n");
348
349         sb.append("\tId: ");
350         sb.append(getId());
351         sb.append("\n\tName: ");
352          sb.append(getName());
353         sb.append("\n\tManufacturer: ");
354          sb.append(getManufacturer());
355         sb.append("\n\tDescription: ");
356          sb.append(getDescription());
357         sb.append("\n\tState: ");
358          sb.append(getState());
359         sb.append("\n\tLast update: ");
360          sb.append(getLastUpdate());
361
362         sb.append("\n\t----");
363         sb.append("\n\tCurrentVersion: \n");
364         if (currentVersion!=null) {
365             sb.append(currentVersion.toString());
366         } else {
367             sb.append("\t\tNot found\n");
368         };
369         sb.append("\n\t----\n");
370         sb.append("\tNewVersion: \n");
371         if (newVersion!=null) {
372             sb.append(newVersion.toString());
373         } else {
374             sb.append("\t\tNot found");
375         }
376
377         return sb.toString();
378     }
379
380
381 }
Popular Tags