View Javadoc

1   /********************************************************************************
2    *InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package base.jdbs;
21  
22  import java.io.File;
23  import java.util.Date;
24  import java.util.Hashtable;
25  import java.util.Observable;
26  
27  import org.apache.log4j.Logger;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.Node;
31  import org.w3c.dom.NodeList;
32  
33  import base.IXMLSaveable;
34  
35  public class Backup extends Observable implements IXMLSaveable {
36  
37  	private static final transient Logger logger = Logger
38  			.getLogger(Backup.class.getName());
39  
40  	/*** The backups' global unique identifier.* */
41  	private String guId;
42  
43  	/*** The backups' name.* */
44  	private String name;
45  
46  	/*** The backups' description.* */
47  	private String description;
48  
49  	/*** The backups' creationDate associated to the life cycle.* */
50  	private Date creationDate;
51  
52  	/*** The backups' expirationDate associated to the life cycle.* */
53  	private Date expirationDate;
54  
55  	/*** The backus'security level, one between private or public.* */
56  	private SecurityLevel securityLevel = SecurityLevel.PRIVATE;
57  
58  	/*** The backupped files.* */
59  	private Hashtable<String, FileDescriptor> file = new Hashtable<String, FileDescriptor>();
60  
61  	/***
62  	 * @param guId
63  	 *            The backups' global unique identifier.
64  	 * @param name
65  	 *            The backups' name.
66  	 * @param description
67  	 *            The backups' description.
68  	 * @param creationDate
69  	 *            The backups' creation date.
70  	 * @param expirationDate
71  	 *            The backups' expiration date.
72  	 * @param securityLevel
73  	 *            The backups' security level.
74  	 */
75  	protected Backup(String guId, String name, String description,
76  			Date creationDate, Date expirationDate, SecurityLevel securityLevel) {
77  		this.guId = guId;
78  		this.name = name;
79  		this.description = description;
80  		this.creationDate = creationDate;
81  		this.expirationDate = expirationDate;
82  		this.securityLevel = securityLevel;
83  	}
84  
85  	/***
86  	 * @return Returns the guId.
87  	 */
88  	public String getGuId() {
89  		return guId;
90  	}
91  
92  	/***
93  	 * @return Returns the description.
94  	 */
95  	public String getDescription() {
96  		return description;
97  	}
98  
99  	/***
100 	 * @param description
101 	 *            The description to set.
102 	 */
103 	public void setDescription(String description) {
104 		this.description = description;
105 	}
106 
107 	/***
108 	 * @return Returns the name.
109 	 */
110 	public String getName() {
111 		return name;
112 	}
113 
114 	/***
115 	 * @param name
116 	 *            The name to set.
117 	 */
118 	public void setName(String name) {
119 		this.name = name;
120 	}
121 
122 	/***
123 	 * @return Returns the creationDate.
124 	 */
125 	public Date getCreationDate() {
126 		return creationDate;
127 	}
128 
129 	/***
130 	 * @return Returns the expirationDate.
131 	 */
132 	public Date getExpirationDate() {
133 		return expirationDate;
134 	}
135 
136 	/***
137 	 * @return the securityLevel
138 	 */
139 	public SecurityLevel getSecurityLevel() {
140 		return securityLevel;
141 	}
142 
143 	/***
144 	 * This method adds a file to the backups' collection.
145 	 * 
146 	 * @param file
147 	 *            The file to be added to the backups'collection.
148 	 */
149 	public void addFile(File file) {
150 		logger.debug("Backup.addFile(" + file + ")");
151 		this.setChanged();
152 		this.file.put(createFileKey(file), new FileDescriptor(file));
153 		this.notifyObservers();
154 	}
155 
156 	/***
157 	 * This method removes a file from the backup's collection.
158 	 * 
159 	 * @param file
160 	 *            The file to be removed from the backup's collection.
161 	 */
162 	public void removeFile(File file) {
163 		logger.debug("Backup.removeFile(" + file + ")");
164 		this.setChanged();
165 		this.file.remove(createFileKey(file));
166 		this.notifyObservers();
167 	}
168 
169 	/***
170 	 * This method adds a set of files to the backups' collection.
171 	 * 
172 	 * @param file
173 	 *            The file set to be added to the backups'collection.
174 	 */
175 	public void addAllFile(File[] file) {
176 		logger.debug("Backup.addAllFile(" + file.length + ")");
177 		this.setChanged();
178 		for (int i = 0; i < file.length; i++)
179 			this.file.put(createFileKey(file[i]), new FileDescriptor(file[i]));
180 		this.notifyObservers();
181 	}
182 
183 	/***
184 	 * This method removes a set of files from the backups' collection.
185 	 * 
186 	 * @param file
187 	 *            The file set to be removed from the backups'collection.
188 	 */
189 	public void removeAllFile(File[] file) {
190 		logger.debug("Backup.removeAllFile(" + file.length + ")");
191 		this.setChanged();
192 		for (int i = 0; i < file.length; i++)
193 			this.file.remove(createFileKey(file[i]));
194 		this.notifyObservers();
195 	}
196 
197 	/***
198 	 * This method returns an array of backups'collected files.
199 	 * 
200 	 * @return The file collection associated to the backup.
201 	 */
202 	public FileDescriptor[] getFileDescriptor() {
203 		return file.values().toArray(new FileDescriptor[0]);
204 	}
205 
206 	/***
207 	 * This method builds a unique key to be associated to a file in the
208 	 * backups'collection.
209 	 * 
210 	 * @param file
211 	 *            The file from which the key must be built.
212 	 * @return A key rappresentation for the input file.
213 	 */
214 	private String createFileKey(File file) {
215 		return file.getAbsolutePath();
216 	}
217 
218 	public Backup(Document document) {
219 		NodeList nodeList = document.getElementsByTagName("Backup");
220 		for (int i = 0; i < nodeList.item(0).getChildNodes().getLength(); i++) {
221 			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
222 					"GUID"))
223 				this.guId = nodeList.item(0).getChildNodes().item(i)
224 						.getAttributes().getNamedItem("value").getNodeValue()
225 						.toString();
226 			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
227 					"Name"))
228 				this.name = nodeList.item(0).getChildNodes().item(i)
229 						.getAttributes().getNamedItem("value").getNodeValue()
230 						.toString();
231 			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
232 					"Description"))
233 				this.description = nodeList.item(0).getChildNodes().item(i)
234 						.getAttributes().getNamedItem("value").getNodeValue()
235 						.toString();
236 			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
237 					"CreationDate"))
238 				this.creationDate = new Date(Long.parseLong(nodeList.item(0)
239 						.getChildNodes().item(i).getAttributes().getNamedItem(
240 								"value").getNodeValue().toString()));
241 			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
242 					"ExpirationDate"))
243 				this.expirationDate = new Date(Long.parseLong(nodeList.item(0)
244 						.getChildNodes().item(i).getAttributes().getNamedItem(
245 								"value").getNodeValue().toString()));
246 			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
247 					"SecurityLevel"))
248 				this.securityLevel = nodeList.item(0).getChildNodes().item(i)
249 						.getAttributes().getNamedItem("value").getNodeValue()
250 						.toString().equalsIgnoreCase(SecurityLevel.level[0]) ? SecurityLevel.PUBLIC
251 						: SecurityLevel.PRIVATE;
252 			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
253 					"File")) {
254 				NodeList fileEntry = nodeList.item(0).getChildNodes().item(i)
255 						.getChildNodes();
256 				for (int k = 0; k < fileEntry.getLength(); k++)
257 					this.addFile(new File(fileEntry.item(k).getAttributes()
258 							.getNamedItem("value").getNodeValue().toString()));
259 			}
260 
261 		}
262 	}
263 
264 	/*
265 	 * (non-Javadoc)
266 	 * 
267 	 * @see base.IXMLSaveable#toXml(org.w3c.dom.Document)
268 	 */
269 	public Node toXml(Document document) {
270 		Element backupElement = document.createElement("Backup");
271 
272 		Element guIdElement = document.createElement("GUID");
273 		guIdElement.setAttribute("value", this.guId);
274 		backupElement.appendChild(guIdElement);
275 
276 		Element nameElement = document.createElement("Name");
277 		nameElement.setAttribute("value", this.name);
278 		backupElement.appendChild(nameElement);
279 
280 		Element descriptionElement = document.createElement("Description");
281 		descriptionElement.setAttribute("value", this.description);
282 		backupElement.appendChild(descriptionElement);
283 
284 		Element creationDateElement = document.createElement("CreationDate");
285 		creationDateElement.setAttribute("value", ""
286 				+ this.creationDate.getTime());
287 		backupElement.appendChild(creationDateElement);
288 
289 		Element expirationDateElement = document
290 				.createElement("ExpirationDate");
291 		expirationDateElement.setAttribute("value", ""
292 				+ this.expirationDate.getTime());
293 		backupElement.appendChild(expirationDateElement);
294 
295 		Element securityLevelElement = document.createElement("SecurityLevel");
296 		securityLevelElement.setAttribute("value", "" + this.securityLevel);
297 		backupElement.appendChild(securityLevelElement);
298 
299 		Element fileElement = document.createElement("File");
300 		FileDescriptor[] file = this.getFileDescriptor();
301 		for (int i = 0; i < file.length; i++) {
302 			Element fileEntryElement = document.createElement("FileEntry");
303 			fileEntryElement.setAttribute("value", "" + file[i].getFile());
304 			fileElement.appendChild(fileEntryElement);
305 		}
306 		backupElement.appendChild(fileElement);
307 
308 		return backupElement;
309 	}
310 
311 	/***
312 	 * @param creationDate
313 	 *            The creationDate to set.
314 	 */
315 	public void setCreationDate(Date creationDate) {
316 		this.creationDate = creationDate;
317 	}
318 
319 	/***
320 	 * @param expirationDate
321 	 *            The expirationDate to set.
322 	 */
323 	public void setExpirationDate(Date expirationDate) {
324 		this.expirationDate = expirationDate;
325 	}
326 
327 	/***
328 	 * @param securityLevel
329 	 *            The securityLevel to set.
330 	 */
331 	public void setSecurityLevel(SecurityLevel securityLevel) {
332 		this.securityLevel = securityLevel;
333 	}
334 
335 }