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.Calendar;
23  import java.util.Date;
24  import java.util.GregorianCalendar;
25  
26  import org.apache.log4j.Logger;
27  
28  import ui.command.Command;
29  import ui.dialog.NewSessionDialog;
30  import ui.panel.SessionCreationPanel;
31  import base.Control;
32  import base.InternetCafeManager;
33  import base.network.Workstation;
34  import base.service.Service;
35  import base.session.Session;
36  import base.session.SessionFactory;
37  import base.user.User;
38  
39  /***
40   * @author skunk
41   * 
42   */
43  public class SaveNewSessionCommand extends Command {
44  
45  	private static final transient Logger logger = Logger
46  			.getLogger(SaveNewSessionCommand.class.getName());
47  
48  	private NewSessionDialog newSessionDialog;
49  
50  	private SessionCreationPanel sessionCreationPanel;
51  
52  	private User user;
53  
54  	private Workstation workstation;
55  
56  	private String description;
57  
58  	private Date startTime;
59  
60  	private Date endTime;
61  
62  	private Service service;
63  
64  	/***
65  	 * @param newSessionDialog
66  	 * @param sessionCreationPanel
67  	 */
68  	public SaveNewSessionCommand(NewSessionDialog newSessionDialog,
69  			SessionCreationPanel sessionCreationPanel) {
70  		super();
71  		// TODO Auto-generated constructor stub
72  		this.newSessionDialog = newSessionDialog;
73  		this.sessionCreationPanel = sessionCreationPanel;
74  	}
75  
76  	/*
77  	 * (non-Javadoc)
78  	 * 
79  	 * @see ui.command.Command#prologo()
80  	 */
81  	@Override
82  	protected void prologo() {
83  		user = sessionCreationPanel.getUser();
84  		workstation = sessionCreationPanel.getWorkstation();
85  		description = newSessionDialog.getSessionDescription();
86  		startTime = newSessionDialog.getSessionStartTime();
87  		endTime = newSessionDialog.getSessionEndTime();
88  		service = newSessionDialog.getSessionService();
89  
90  		GregorianCalendar startTimeGC = new GregorianCalendar();
91  		startTimeGC.setTime(startTime);
92  		GregorianCalendar endTimeGC = new GregorianCalendar();
93  		endTimeGC.setTime(endTime);
94  		startTime = new Date(startTimeGC.getTimeInMillis());
95  		endTime = new Date(new GregorianCalendar(
96  				startTimeGC.get(Calendar.YEAR),
97  				startTimeGC.get(Calendar.MONTH),
98  				startTimeGC.get(Calendar.DATE),
99  				endTimeGC.get(Calendar.HOUR) + 12, endTimeGC
100 						.get(Calendar.MINUTE), endTimeGC.get(Calendar.SECOND))
101 				.getTimeInMillis());
102 
103 		if (isValidSessionData(user, workstation, description, startTime,
104 				endTime, service))
105 			setStatus(EXECUTE_STATUS);
106 		else
107 			setStatus(ERROR_STATUS);
108 	}
109 
110 	private boolean isValidSessionData(User user, Workstation workstation,
111 			String description, Date startTime, Date endTime, Service service) {
112 		if (user == null) {
113 			this.sessionCreationPanel.setUserError(true);
114 			return false;
115 		} else
116 			this.sessionCreationPanel.setUserError(false);
117 
118 		if (workstation == null) {
119 			this.sessionCreationPanel.setWorkstationError(true);
120 			return false;
121 		} else
122 			this.sessionCreationPanel.setWorkstationError(false);
123 
124 		if (!Control.isValidSessionDate(startTime, endTime)) {
125 			this.newSessionDialog.setSessionDateError(true);
126 			return false;
127 		} else
128 			this.newSessionDialog.setSessionDateError(false);
129 
130 		return true;
131 	}
132 
133 	/*
134 	 * (non-Javadoc)
135 	 * 
136 	 * @see ui.command.Command#execution()
137 	 */
138 	@Override
139 	protected void execution() throws Exception {
140 		switch (getStatus()) {
141 		case ERROR_STATUS:
142 			break;
143 		case VETOED_STATUS:
144 			break;
145 		case EXECUTE_STATUS:
146 			logger.debug("NewSession : " + "start " + startTime + " end " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
147 					+ endTime + " valid ? " //$NON-NLS-1$
148 					+ Control.isValidSessionDate(startTime, endTime));
149 			Session session = SessionFactory.newSession(user, workstation,
150 					description, startTime, endTime, service);
151 			InternetCafeManager.getInstance().addSession(session);
152 			setStatus(SUCCESS_STATUS);
153 			break;
154 		}
155 	}
156 }