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.net.Inet4Address;
23  
24  import ui.command.Command;
25  import ui.panel.WorkstationPanel;
26  import base.Control;
27  import base.network.NetworkManager;
28  import base.network.Workstation;
29  import base.network.WorkstationFactory;
30  
31  public class SaveClientWorkstationCommand extends Command {
32  
33  	private WorkstationPanel workstationPanel;
34  
35  	private Workstation workstation;
36  
37  	private boolean createWorkstation;
38  
39  	private String name;
40  
41  	private String type;
42  
43  	private String location;
44  
45  	private String description;
46  
47  	private Inet4Address address;
48  
49  	private int port;
50  
51  	private boolean inUse;
52  
53  	/***
54  	 * @param workstationPanel
55  	 * @param workstation
56  	 * @param createWorkstation
57  	 */
58  	public SaveClientWorkstationCommand(WorkstationPanel workstationPanel,
59  			Workstation workstation, boolean createWorkstation) {
60  		super();
61  		// TODO Auto-generated constructor stub
62  		this.workstationPanel = workstationPanel;
63  		this.workstation = workstation;
64  		this.createWorkstation = createWorkstation;
65  	}
66  
67  	/*
68  	 * (non-Javadoc)
69  	 * 
70  	 * @see ui.command.Command#prologo()
71  	 */
72  	@Override
73  	protected void prologo() {
74  		name = getWorkstationPanel().getWorkstationName();
75  		type = getWorkstationPanel().getWorkstationType();
76  		location = getWorkstationPanel().getWorkstationLocation();
77  		description = getWorkstationPanel().getWorkstationDescription();
78  		address = getWorkstationPanel().getWorkstationAddress();
79  		port = getWorkstationPanel().getWorkstationPort();
80  		inUse = getWorkstationPanel().getWorkstationInUse();
81  
82  		if (isValidWorkstationData(name, type, location, description, address,
83  				port, inUse))
84  			setStatus(EXECUTE_STATUS);
85  		else
86  			setStatus(ERROR_STATUS);
87  	}
88  
89  	private boolean isValidWorkstationData(String name, String type,
90  			String location, String description, Inet4Address address,
91  			int port, boolean inUse) {
92  		if (name == null || name.length() == 0) {
93  			this.getWorkstationPanel().setNameError(true);
94  			return false;
95  		} else
96  			this.getWorkstationPanel().setNameError(false);
97  		if (!Control.isValidUDPPort(port)) {
98  			this.getWorkstationPanel().setPortError(true);
99  			return false;
100 		}
101 		this.getWorkstationPanel().setPortError(false);
102 		return true;
103 	}
104 
105 	/*
106 	 * (non-Javadoc)
107 	 * 
108 	 * @see ui.command.Command#execution()
109 	 */
110 	@Override
111 	protected void execution() throws Exception {
112 		switch (getStatus()) {
113 		case ERROR_STATUS:
114 			break;
115 		case VETOED_STATUS:
116 			break;
117 		case EXECUTE_STATUS:
118 			if (createWorkstation) {
119 				workstation = WorkstationFactory.newWorkstation(name, type,
120 						description, address, port, inUse);
121 				NetworkManager.getInstance().addClientWorkstation(workstation);
122 				setStatus(SUCCESS_STATUS);
123 			} else {
124 				workstation.setName(name);
125 				workstation.setType(type);
126 				workstation.setLocation(location);
127 				workstation.setDescription(description);
128 				workstation.setAddress(address);
129 				workstation.setPort(port);
130 				workstation.setInUse(inUse);
131 				setStatus(SUCCESS_STATUS);
132 			}
133 			break;
134 		}
135 	}
136 
137 	/***
138 	 * @return Returns the workstationPanel.
139 	 */
140 	private WorkstationPanel getWorkstationPanel() {
141 		return workstationPanel;
142 	}
143 
144 }