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.backup;
21  
22  import java.io.File;
23  import java.util.Date;
24  
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  import org.w3c.dom.Node;
28  import org.w3c.dom.NodeList;
29  
30  import base.ICXmlTags;
31  
32  public class Backup {
33  	private final int id;
34  
35  	private String name;
36  
37  	private String description;
38  
39  	private Date date;
40  
41  	private String dbLocationPath;
42  
43  	private String zipLocationPath;
44  
45  	private long size = -1;
46  
47  	/***
48  	 * @param id
49  	 *            The backup's id.
50  	 * @param name
51  	 *            The backup's name.
52  	 * @param description
53  	 *            The backup's description.
54  	 * @param date
55  	 *            The backup's date.
56  	 * @param dbLocationPath
57  	 *            The backup's database file dbLocationPath.
58  	 * @param zipLocationPath
59  	 */
60  	protected Backup(int id, String name, String description, Date date,
61  			String dbLocationPath, String zipLocationPath) {
62  		this.id = id;
63  		this.name = name;
64  		this.description = description;
65  		this.date = date;
66  		this.dbLocationPath = dbLocationPath;
67  		this.zipLocationPath = zipLocationPath;
68  	}
69  
70  	public Backup(Document document) {
71  		NodeList detailsNodeList = document
72  				.getElementsByTagName(ICXmlTags.BACKUP_DETAILS_TAG);
73  		Node detailsNode = detailsNodeList.item(0);
74  
75  		this.id = new Integer(detailsNode.getAttributes().getNamedItem(
76  				ICXmlTags.BACKUP_ID_ATTRIBUTE).getNodeValue());
77  		this.name = detailsNode.getAttributes().getNamedItem(
78  				ICXmlTags.BACKUP_NAME_ATTRIBUTE).getNodeValue();
79  		this.date = new Date(new Long(detailsNode.getAttributes().getNamedItem(
80  				ICXmlTags.BACKUP_DATE_ATTRIBUTE).getNodeValue()));
81  
82  		for (int i = 0; i < detailsNode.getChildNodes().getLength(); i++) {
83  			if (detailsNode.getChildNodes().item(i).getNodeName().equals(
84  					ICXmlTags.BACKUP_DB_LOCATION_PATH_TAG)) {
85  				this.dbLocationPath = detailsNode.getChildNodes().item(i)
86  						.getAttributes().getNamedItem(
87  								ICXmlTags.BACKUP_DB_LOCATION_PATH_ATTRIBUTE)
88  						.getNodeValue();
89  			}
90  
91  			if (detailsNode.getChildNodes().item(i).getNodeName().equals(
92  					ICXmlTags.BACKUP_DESCRIPTION_TAG)) {
93  				this.description = detailsNode.getChildNodes().item(i)
94  						.getAttributes().getNamedItem(
95  								ICXmlTags.BACKUP_DESCRIPTION_ATTRIBUTE)
96  						.getNodeValue();
97  			}
98  		}
99  	}
100 
101 	/***
102 	 * @return Returns the date.
103 	 */
104 	public Date getDate() {
105 		return date;
106 	}
107 
108 	/***
109 	 * @param date
110 	 *            The date to set.
111 	 */
112 	protected void setDate(Date date) {
113 		this.date = date;
114 	}
115 
116 	/***
117 	 * @return Returns the description.
118 	 */
119 	public String getDescription() {
120 		return description;
121 	}
122 
123 	/***
124 	 * @param description
125 	 *            The description to set.
126 	 */
127 	protected void setDescription(String description) {
128 		this.description = description;
129 	}
130 
131 	/***
132 	 * @return Returns the name.
133 	 */
134 	public String getName() {
135 		return name;
136 	}
137 
138 	/***
139 	 * @param name
140 	 *            The name to set.
141 	 */
142 	protected void setName(String name) {
143 		this.name = name;
144 	}
145 
146 	/***
147 	 * @return Returns the dbLocationPath.
148 	 */
149 	public String getDbLocationPath() {
150 		return dbLocationPath;
151 	}
152 
153 	/***
154 	 * @return Returns the id.
155 	 */
156 	public int getId() {
157 		return id;
158 	}
159 
160 	public String getBackupFileName() {
161 		return this.getDbLocationPath().substring(
162 				this.getDbLocationPath().lastIndexOf("" + File.pathSeparator),
163 				this.getDbLocationPath().length());
164 	}
165 
166 	/***
167 	 * @return Returns the zipLocationPath.
168 	 */
169 	public String getZipLocationPath() {
170 		return zipLocationPath;
171 	}
172 
173 	/***
174 	 * @param currentBackupLocationPath
175 	 *            The currentBackupLocationPath to set.
176 	 */
177 	public void setCurrentBackupLocationPath(String currentBackupLocationPath) {
178 		this.zipLocationPath = currentBackupLocationPath;
179 	}
180 
181 	public Node toXml(Document document) {
182 		Element detailsElement = document
183 				.createElement(ICXmlTags.BACKUP_DETAILS_TAG);
184 
185 		detailsElement.setAttribute(ICXmlTags.BACKUP_ID_ATTRIBUTE, ""
186 				+ this.getId());
187 		detailsElement.setAttribute(ICXmlTags.BACKUP_NAME_ATTRIBUTE, this
188 				.getName());
189 		detailsElement.setAttribute(ICXmlTags.BACKUP_DATE_ATTRIBUTE, ""
190 				+ this.getDate().getTime());
191 
192 		Element originalDestinationPathElement = document
193 				.createElement(ICXmlTags.BACKUP_DB_LOCATION_PATH_TAG);
194 
195 		originalDestinationPathElement.setAttribute(
196 				ICXmlTags.BACKUP_DB_LOCATION_PATH_ATTRIBUTE, this
197 						.getDbLocationPath());
198 		detailsElement.appendChild(originalDestinationPathElement);
199 
200 		Element descriptionElement = document
201 				.createElement(ICXmlTags.BACKUP_DESCRIPTION_TAG);
202 
203 		descriptionElement.setAttribute(ICXmlTags.BACKUP_DESCRIPTION_ATTRIBUTE,
204 				this.getDescription());
205 		detailsElement.appendChild(descriptionElement);
206 
207 		return detailsElement;
208 	}
209 
210 	/***
211 	 * @param dbLocationPath
212 	 *            The dbLocationPath to set.
213 	 */
214 	protected void setDbLocationPath(String dbLocationPath) {
215 		this.dbLocationPath = dbLocationPath;
216 	}
217 
218 	/***
219 	 * @param zipLocationPath
220 	 *            The zipLocationPath to set.
221 	 */
222 	protected void setZipLocationPath(String zipLocationPath) {
223 		this.zipLocationPath = zipLocationPath;
224 	}
225 
226 	/***
227 	 * @return Returns the size.
228 	 */
229 	public long getSize() {
230 		if (getZipLocationPath() == null) {
231 			return size = -1;
232 		}
233 
234 		File zipFile = new File(getZipLocationPath());
235 
236 		if (!zipFile.exists()) {
237 			return size = -1;
238 		}
239 
240 		return (size != -1) ? size : (size = BackupFactory
241 				.fileSizeInKB(zipFile));
242 	}
243 }