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 ui.command.IO;
21  
22  import java.util.Date;
23  
24  import ui.command.Command;
25  import ui.panel.DocumentPanel;
26  import base.Control;
27  import base.user.Document;
28  
29  public class SaveUserDocumentCommand extends Command {
30  
31  	private DocumentPanel documentPanel;
32  
33  	private Document document;
34  
35  	private boolean createDocument;
36  
37  	private String type;
38  
39  	private String number;
40  
41  	private String releaseAuthority;
42  
43  	private String description;
44  
45  	private Date releaseDate;
46  
47  	private Date expirationDate;
48  
49  	private String imagePath;
50  
51  	/***
52  	 * @param documentPanel
53  	 * @param document
54  	 * @param createDocument
55  	 */
56  	public SaveUserDocumentCommand(DocumentPanel documentPanel,
57  			Document document, boolean createDocument) {
58  		this.documentPanel = documentPanel;
59  		this.document = document;
60  		this.createDocument = createDocument;
61  	}
62  
63  	/*
64  	 * (non-Javadoc)
65  	 * 
66  	 * @see ui.command.Command#prologo()
67  	 */
68  	@Override
69  	protected void prologo() {
70  		type = getDocumentPanel().getDocumentType();
71  		number = getDocumentPanel().getDocumentNumber();
72  		releaseAuthority = getDocumentPanel().getDocumentReleaseAuthority();
73  		description = getDocumentPanel().getDocumentDescription();
74  		releaseDate = getDocumentPanel().getDocumentRelease();
75  		expirationDate = getDocumentPanel().getDocumentExpiration();
76  		imagePath = getDocumentPanel().getDocumentImagePath();
77  		if (isValidUserDocumentData(type, number, releaseAuthority,
78  				description, releaseDate, expirationDate, imagePath))
79  			setStatus(EXECUTE_STATUS);
80  		else
81  			setStatus(ERROR_STATUS);
82  	}
83  
84  	private boolean isValidUserDocumentData(String type, String number,
85  			String releaseAuthority, String description, Date releaseDate,
86  			Date expirationDate, String imagePath) {
87  
88  		if (!Control.isValidDocumentNumber(type, number)) {
89  			getDocumentPanel().setDocumentNumberError(true);
90  			return false;
91  		} else
92  			getDocumentPanel().setDocumentNumberError(false);
93  
94  		if (!Control.isValidDocumentDate(releaseDate, expirationDate)) {
95  			getDocumentPanel().setDocumentDateError(true);
96  			return false;
97  		} else
98  			getDocumentPanel().setDocumentDateError(false);
99  
100 		return true;
101 	}
102 
103 	/*
104 	 * (non-Javadoc)
105 	 * 
106 	 * @see ui.command.Command#execution()
107 	 */
108 	@Override
109 	protected void execution() throws Exception {
110 		switch (getStatus()) {
111 		case ERROR_STATUS:
112 			break;
113 		case VETOED_STATUS:
114 			break;
115 		case EXECUTE_STATUS:
116 			if (createDocument) {
117 				document = new Document(type, number, releaseAuthority,
118 						description, releaseDate, expirationDate, imagePath);
119 				setStatus(SUCCESS_STATUS);
120 			} else {
121 				document.setType(type);
122 				document.setNumber(number);
123 				document.setAuthority(releaseAuthority);
124 				document.setDescription(description);
125 				document.setRelease(releaseDate);
126 				document.setExpiration(expirationDate);
127 				document.setImagePath(imagePath);
128 				setStatus(SUCCESS_STATUS);
129 			}
130 			break;
131 		}
132 	}
133 	
134 	/***
135 	 * @return Returns the documentPanel.
136 	 */
137 	private DocumentPanel getDocumentPanel() {
138 		return documentPanel;
139 	}
140 
141 }