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 ui.command.Command;
23  import ui.panel.ServicePanel;
24  import base.Control;
25  import base.InternetCafeManager;
26  import base.service.Service;
27  import base.service.ServiceFactory;
28  
29  public class SaveServiceCommand extends Command {
30  
31  	private ServicePanel servicePanel;
32  
33  	private Service service;
34  
35  	private boolean createService;
36  
37  	private String name;
38  
39  	private String description;
40  
41  	private String rateType;
42  
43  	private double baseRate;
44  
45  	private int minDurationInMinute;
46  
47  	private boolean careAboutMinDuration;
48  
49  	private int maxDurationInMinute;
50  
51  	private boolean careAboutMaxDuration;
52  
53  	/***
54  	 * @param servicePanel
55  	 * @param createService
56  	 */
57  	public SaveServiceCommand(ServicePanel servicePanel, Service service,
58  			boolean createService) {
59  		super();
60  		// TODO Auto-generated constructor stub
61  		this.servicePanel = servicePanel;
62  		this.service = service;
63  		this.createService = createService;
64  	}
65  
66  	/*
67  	 * (non-Javadoc)
68  	 * 
69  	 * @see ui.command.Command#prologo()
70  	 */
71  	@Override
72  	protected void prologo() {
73  		name = getServicePanel().getServiceName();
74  		description = getServicePanel().getServiceDescription();
75  		rateType = getServicePanel().getServiceRateType();
76  		baseRate = getServicePanel().getServiceBaseRate();
77  		minDurationInMinute = getServicePanel().getServiceMinDurationInMinute();
78  		careAboutMinDuration = getServicePanel()
79  				.getServiceCareAboutMinDuration();
80  		maxDurationInMinute = getServicePanel().getServiceMaxDurationInMinute();
81  		careAboutMaxDuration = getServicePanel()
82  				.getServiceCareAboutMaxDuration();
83  		if (isValidServiceData(name, description, rateType, baseRate,
84  				minDurationInMinute, careAboutMinDuration, maxDurationInMinute,
85  				careAboutMaxDuration))
86  			setStatus(EXECUTE_STATUS);
87  		else
88  			setStatus(ERROR_STATUS);
89  	}
90  
91  	private boolean isValidServiceData(String name, String description,
92  			String rateType, double baseRate, int minDurationInMinute,
93  			boolean careAboutMinDuration, int maxDurationInMinute,
94  			boolean careAboutMaxDuration) {
95  		if (name == null || name.length() == 0) {
96  			this.getServicePanel().setNameError(true);
97  			return false;
98  		} else
99  			this.getServicePanel().setNameError(false);
100 
101 		if (baseRate < 0) {
102 			this.getServicePanel().setBaseRateError(true);
103 			return false;
104 		} else
105 			this.getServicePanel().setBaseRateError(false);
106 
107 		if (!Control
108 				.isValidServiceMinMaxDuration(minDurationInMinute,
109 						careAboutMinDuration, maxDurationInMinute,
110 						careAboutMaxDuration)) {
111 			this.getServicePanel().setMinMaxDurationError(true);
112 			return false;
113 		} else
114 			this.getServicePanel().setMinMaxDurationError(false);
115 
116 		return true;
117 	}
118 
119 	/*
120 	 * (non-Javadoc)
121 	 * 
122 	 * @see ui.command.Command#execution()
123 	 */
124 	@Override
125 	protected void execution() throws Exception {
126 		switch (getStatus()) {
127 		case ERROR_STATUS:
128 			break;
129 		case VETOED_STATUS:
130 			break;
131 		case EXECUTE_STATUS:
132 			if (createService) {
133 				service = ServiceFactory.newService(name, description,
134 						rateType, baseRate, minDurationInMinute,
135 						careAboutMinDuration, maxDurationInMinute,
136 						careAboutMaxDuration);
137 				InternetCafeManager.getInstance().addService(service);
138 				setStatus(SUCCESS_STATUS);
139 			} else {
140 				service.setName(name);
141 				service.setDescription(description);
142 				service.setRateType(rateType);
143 				service.setBaseRate(baseRate);
144 				service.setMinDurationInMinute(minDurationInMinute);
145 				service.setCareAboutMinDuration(careAboutMinDuration);
146 				service.setMaxDurationInMinute(maxDurationInMinute);
147 				service.setCareAboutMaxDuration(careAboutMaxDuration);
148 				setStatus(SUCCESS_STATUS);
149 			}
150 		}
151 	}
152 	
153 	/***
154 	 * @return Returns the servicePanel.
155 	 */
156 	private ServicePanel getServicePanel() {
157 		return servicePanel;
158 	}
159 
160 }