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.user;
21  
22  import java.util.Date;
23  
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  
27  import base.ICXmlTags;
28  import base.IXMLSaveable;
29  
30  public class Document implements IXMLSaveable {
31  
32  	private String type = DocumentType.DOCUMENT_TYPE[0];
33  
34  	private String number = "";
35  
36  	private String description = "";
37  
38  	private Date release = new Date();
39  
40  	private Date expiration = new Date();
41  
42  	private String imagePath = null;
43  
44  	private String releaseAuthority = DocumentReleaseAuthority.DOCUMENT_RELEASE_AUTHORITY[0];
45  
46  	/***
47  	 * @param type
48  	 *            The document's type.
49  	 * @param number
50  	 *            The document's number.
51  	 * @param releaseAuthority
52  	 *            The document's release authority.
53  	 * @param description
54  	 *            The document's description.
55  	 * @param release
56  	 *            The document's release date.
57  	 * @param expiration
58  	 *            The document's expiration date.
59  	 * @param imagePath
60  	 *            The document's image path.
61  	 * 
62  	 */
63  	public Document(String type, String number, String releaseAuthority,
64  			String description, Date release, Date expiration, String imagePath) {
65  		this.releaseAuthority = releaseAuthority;
66  		this.description = description;
67  		this.release = release;
68  		this.number = number;
69  		this.expiration = expiration;
70  		this.type = type;
71  		this.imagePath = imagePath;
72  	}
73  
74  	public Document() {
75  		// TODO Auto-generated constructor stub
76  	}
77  
78  	/***
79  	 * @return Returns the description.
80  	 */
81  	public String getDescription() {
82  		return description;
83  	}
84  
85  	/***
86  	 * @param description
87  	 *            The description to set.
88  	 */
89  	public void setDescription(String description) {
90  		this.description = description;
91  	}
92  
93  	/***
94  	 * @return Returns the expiration.
95  	 */
96  	public Date getExpiration() {
97  		return expiration;
98  	}
99  
100 	/***
101 	 * @param expiration
102 	 *            The expiration to set.
103 	 */
104 	public void setExpiration(Date expiration) {
105 		this.expiration = expiration;
106 	}
107 
108 	/***
109 	 * @return Returns the imagePath.
110 	 */
111 	public String getImagePath() {
112 		return imagePath;
113 	}
114 
115 	/***
116 	 * @param imagePath
117 	 *            The imagePath to set.
118 	 */
119 	public void setImagePath(String imagePath) {
120 		this.imagePath = imagePath;
121 	}
122 
123 	/***
124 	 * @return Returns the release.
125 	 */
126 	public Date getRelease() {
127 		return release;
128 	}
129 
130 	/***
131 	 * @param release
132 	 *            The release to set.
133 	 */
134 	public void setRelease(Date release) {
135 		this.release = release;
136 	}
137 
138 	/***
139 	 * @return Returns the releaseAuthority.
140 	 */
141 	public String getReleaseAuthority() {
142 		return releaseAuthority;
143 	}
144 
145 	/***
146 	 * @return Returns the number.
147 	 */
148 	public String getNumber() {
149 		return number;
150 	}
151 
152 	/***
153 	 * @return Returns the type.
154 	 */
155 	public String getType() {
156 		return type;
157 	}
158 
159 	/*
160 	 * (non-Javadoc)
161 	 * 
162 	 * @see java.lang.Object#toString()
163 	 */
164 	@Override
165 	public String toString() {
166 		StringBuffer sb = new StringBuffer();
167 		sb.append("DOCUMENT");
168 		sb.append("\n");
169 		sb.append("type:" + type);
170 		sb.append("\n");
171 		sb.append("number: " + number);
172 		sb.append("\n");
173 		sb.append("description: " + description);
174 		sb.append("\n");
175 		sb.append("release: " + release);
176 		sb.append("\n");
177 		sb.append("expiration: " + expiration);
178 		sb.append("\n");
179 		sb.append("image: " + imagePath);
180 		sb.append("\n");
181 		sb.append("authority: " + releaseAuthority);
182 		return sb.toString();
183 	}
184 
185 	/***
186 	 * @param releaseAuthority
187 	 *            The authority to set.
188 	 */
189 	public void setAuthority(String releaseAuthority) {
190 		this.releaseAuthority = releaseAuthority;
191 	}
192 
193 	/***
194 	 * @param number
195 	 *            The number to set.
196 	 */
197 	public void setNumber(String number) {
198 		this.number = number;
199 	}
200 
201 	/***
202 	 * @param type
203 	 *            The type to set.
204 	 */
205 	public void setType(String type) {
206 		this.type = type;
207 	}
208 
209 	public Node toXml(org.w3c.dom.Document document) {
210 		Element documentElement = document
211 				.createElement(ICXmlTags.IC_DOCUMENT_TAG);
212 
213 		Element typeElement = document
214 				.createElement(ICXmlTags.IC_DOCUMENT_TYPE_TAG);
215 		typeElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, this.type);
216 		documentElement.appendChild(typeElement);
217 
218 		Element numberElement = document
219 				.createElement(ICXmlTags.IC_DOCUMENT_NUMBER_TAG);
220 		numberElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, this.number);
221 		documentElement.appendChild(numberElement);
222 
223 		Element descriptionElement = document
224 				.createElement(ICXmlTags.IC_DOCUMENT_DESCRIPTION_TAG);
225 		descriptionElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
226 				this.description);
227 		documentElement.appendChild(descriptionElement);
228 
229 		Element releaseAuthorityElement = document
230 				.createElement(ICXmlTags.IC_DOCUMENT_RELEASE_AUTHORITY_TAG);
231 		releaseAuthorityElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
232 				this.releaseAuthority.toString());
233 		documentElement.appendChild(releaseAuthorityElement);
234 
235 		Element releaseElement = document
236 				.createElement(ICXmlTags.IC_DOCUMENT_RELEASE_TAG);
237 		releaseElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, this.release
238 				.toString());
239 		releaseElement.setAttribute(ICXmlTags.IC_LONG_VALUE_ATTRIBUTE, ""+this.release.getTime());
240 		documentElement.appendChild(releaseElement);
241 
242 		Element expirationElement = document
243 				.createElement(ICXmlTags.IC_DOCUMENT_EXPIRATION_TAG);
244 		expirationElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
245 				this.expiration.toString());
246 		expirationElement.setAttribute(ICXmlTags.IC_LONG_VALUE_ATTRIBUTE, ""+this.expiration.getTime());
247 		
248 		documentElement.appendChild(expirationElement);
249 
250 		Element imagePathElement = document
251 				.createElement(ICXmlTags.IC_DOCUMENT_IMAGE_PATH_TAG);
252 		imagePathElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
253 				this.imagePath);
254 		documentElement.appendChild(imagePathElement);
255 
256 		return documentElement;
257 	}
258 
259 	public base.user.Document fromXml(Node node) {
260 		
261 		String type="";		
262 		String number="";
263 		String description="";
264 		Date release=null;;
265 		Date expiration=null;;
266 		String imagePath="";
267 		String releaseAuthority="";
268 		
269 		for(int i=0;i<node.getChildNodes().getLength();i++){
270 			if(node.getChildNodes().item(i).getNodeName().equalsIgnoreCase(ICXmlTags.IC_DOCUMENT_TYPE_TAG))
271 				type = node.getChildNodes().item(i).getAttributes().getNamedItem(ICXmlTags.IC_VALUE_ATTRIBUTE).getNodeValue();
272 			if(node.getChildNodes().item(i).getNodeName().equalsIgnoreCase(ICXmlTags.IC_DOCUMENT_NUMBER_TAG))
273 				number = node.getChildNodes().item(i).getAttributes().getNamedItem(ICXmlTags.IC_VALUE_ATTRIBUTE).getNodeValue();
274 			if(node.getChildNodes().item(i).getNodeName().equalsIgnoreCase(ICXmlTags.IC_DOCUMENT_DESCRIPTION_TAG))
275 				description = node.getChildNodes().item(i).getAttributes().getNamedItem(ICXmlTags.IC_VALUE_ATTRIBUTE).getNodeValue();
276 			if(node.getChildNodes().item(i).getNodeName().equalsIgnoreCase(ICXmlTags.IC_DOCUMENT_RELEASE_AUTHORITY_TAG))
277 				releaseAuthority = node.getChildNodes().item(i).getAttributes().getNamedItem(ICXmlTags.IC_VALUE_ATTRIBUTE).getNodeValue();
278 			if(node.getChildNodes().item(i).getNodeName().equalsIgnoreCase(ICXmlTags.IC_DOCUMENT_RELEASE_TAG))
279 				release = new Date(Long.parseLong(node.getChildNodes().item(i).getAttributes().getNamedItem(ICXmlTags.IC_LONG_VALUE_ATTRIBUTE).getNodeValue()));
280 			if(node.getChildNodes().item(i).getNodeName().equalsIgnoreCase(ICXmlTags.IC_DOCUMENT_EXPIRATION_TAG))
281 				expiration = new Date(Long.parseLong(node.getChildNodes().item(i).getAttributes().getNamedItem(ICXmlTags.IC_LONG_VALUE_ATTRIBUTE).getNodeValue()));
282 			if(node.getChildNodes().item(i).getNodeName().equalsIgnoreCase(ICXmlTags.IC_DOCUMENT_IMAGE_PATH_TAG))
283 				imagePath = node.getChildNodes().item(i).getAttributes().getNamedItem(ICXmlTags.IC_VALUE_ATTRIBUTE).getNodeValue();
284 		}
285 		
286 		return new Document(type, number, releaseAuthority, description, release, expiration, imagePath);
287 	}
288 }