View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo 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 PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.commands;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.commons.lang.math.IntRange;
28  import org.promotego.beans.Address;
29  import org.promotego.beans.Location;
30  import org.promotego.beans.LocationType;
31  import org.promotego.beans.User;
32  import org.promotego.logic.storehours.Day;
33  import org.promotego.logic.storehours.HourSpecification;
34  import org.promotego.logic.storehours.StoreHours;
35  
36  /***
37   * @author alf
38   *
39   */
40  public class LocationCommandBean
41  {
42  	private static final int SECONDS_IN_DAY = 86400;
43  	
44  	private Location m_location;
45  	private int [] m_openHours;
46  	private int [] m_openMinutes;
47  	private int [] m_openAmpms;
48  	private int [] m_closeHours;
49  	private int [] m_closeMinutes;
50  	private int [] m_closeAmpms;
51  
52  	public LocationCommandBean()
53  	{
54  		initHours();
55  	}
56  	
57  	private void initHours()
58  	{
59  		m_openHours = new int[] {-1, -1, -1, -1, -1, -1, -1};
60  		m_openMinutes = new int[] {-1, -1, -1, -1, -1, -1, -1};
61  		m_openAmpms = new int[] {0, 0, 0, 0, 0, 0, 0};
62  		m_closeHours = new int[] {-1, -1, -1, -1, -1, -1, -1};
63  		m_closeMinutes = new int[] {-1, -1, -1, -1, -1, -1, -1};
64  		m_closeAmpms = new int[] {0, 0, 0, 0, 0, 0, 0};
65  	}
66  
67  	public Location getLocation()
68  	{
69  		setStoreHours();
70  		return m_location;
71  	}
72  	
73  	private void setStoreHours()
74  	{
75  		List<HourSpecification> hourSpecList = new ArrayList<HourSpecification>(7);
76  		
77  		for (int i=0; i<7; i++)
78  		{
79  			if (m_openHours[i] < 0 || m_closeHours[i] < 0)
80  			{
81  				// No hour set for opening or closing on this day.
82  				continue;
83  			}
84  			
85  			Set<Day> daySet = Collections.singleton(Day.values()[i]);
86  			
87  			int openTime = m_openHours[i];
88  			if (m_openMinutes[i] >= 0)
89  			{
90  				openTime += m_openMinutes[i];
91  			}
92  			
93  			openTime += m_openAmpms[i];
94  			
95  			int closeTime = m_closeHours[i];
96  			if (m_closeMinutes[i] >= 0)
97  			{
98  				closeTime += m_closeMinutes[i];
99  			}
100 			
101 			closeTime += m_closeAmpms[i];
102 			
103 			if (closeTime <= openTime)
104 			{
105 				closeTime += SECONDS_IN_DAY;
106 			}
107 			
108 			List<IntRange> rangeList = Collections.singletonList(new IntRange(openTime, closeTime));
109 			
110 			hourSpecList.add(new HourSpecification(daySet, rangeList));
111 		}
112 		
113 		m_location.setStoreHours(new StoreHours(hourSpecList));
114 	}
115 
116 	public void setLocation(Location location)
117 	{
118 		m_location = location;
119 		getStoreHoursFromLocation();
120 	}
121 	
122 	private void getStoreHoursFromLocation()
123 	{
124 		initHours();
125 		
126 		StoreHours theHours = m_location.getStoreHours();
127 		
128 		if (theHours == null)
129 		{
130 			return;
131 		}
132 		
133 		Collection<HourSpecification> hourSpecs = theHours.getHourSpecifications();
134 		
135 		for (HourSpecification thisSpec: hourSpecs)
136 		{
137 			for (Day thisDay : thisSpec.getDays())
138 			{
139 				for (IntRange thisRange : thisSpec.getSecondRanges())
140 				{
141 					int dayNum = thisDay.ordinal();
142 					
143 					assert m_openHours[dayNum] == -1 : "Only one range per day supported at this time";
144 					
145 					int openSeconds = thisRange.getMinimumInteger();
146 					
147 					m_openMinutes[dayNum] = openSeconds%3600;
148 					m_openHours[dayNum] = openSeconds%(SECONDS_IN_DAY/2) - m_openMinutes[dayNum];
149 					m_openAmpms[dayNum] = (openSeconds>SECONDS_IN_DAY/2)?(SECONDS_IN_DAY/2):0;
150 					
151 					int closeSeconds = thisRange.getMaximumInteger();
152 					
153 					if (closeSeconds > SECONDS_IN_DAY)
154 					{
155 						closeSeconds -= SECONDS_IN_DAY;
156 					}
157 					
158 					m_closeMinutes[dayNum] = closeSeconds%3600;
159 					m_closeHours[dayNum] = closeSeconds%(SECONDS_IN_DAY/2) - m_closeMinutes[dayNum];
160 					m_closeAmpms[dayNum] = (closeSeconds>SECONDS_IN_DAY/2)?(SECONDS_IN_DAY/2):0;
161 				}
162 			}
163 		}
164 	}
165 
166 	public int[] getOpenHours()
167 	{
168 		return m_openHours;
169 	}
170 
171 	public void setOpenHours(int[] openHours)
172 	{
173 		m_openHours = openHours;
174 	}
175 
176 	public int[] getOpenMinutes()
177 	{
178 		return m_openMinutes;
179 	}
180 
181 	public void setOpenMinutes(int[] openMinutes)
182 	{
183 		m_openMinutes = openMinutes;
184 	}
185 
186 	public int[] getCloseHours()
187 	{
188 		return m_closeHours;
189 	}
190 
191 	public void setCloseHours(int[] closeHours)
192 	{
193 		m_closeHours = closeHours;
194 	}
195 
196 	public int[] getCloseMinutes()
197 	{
198 		return m_closeMinutes;
199 	}
200 
201 	public void setCloseMinutes(int[] closeMinutes)
202 	{
203 		m_closeMinutes = closeMinutes;
204 	}
205 
206 	public Address getAddress()
207 	{
208 		return m_location.getAddress();
209 	}
210 
211 	public Long getId()
212 	{
213 		return m_location.getId();
214 	}
215 
216 	public String getName()
217 	{
218 		return m_location.getName();
219 	}
220 
221 	public User getOwner()
222 	{
223 		return m_location.getOwner();
224 	}
225 
226 	public String getPhoneNumber()
227 	{
228 		return m_location.getPhoneNumber();
229 	}
230 
231 	public StoreHours getStoreHours()
232 	{
233 		return m_location.getStoreHours();
234 	}
235 
236 	public String getStoreHoursString()
237 	{
238 		return m_location.getStoreHoursString();
239 	}
240 
241 	public LocationType getType()
242 	{
243 		return m_location.getType();
244 	}
245 
246 	public void setAddress(Address address)
247 	{
248 		m_location.setAddress(address);
249 	}
250 
251 	public void setId(Long id)
252 	{
253 		m_location.setId(id);
254 	}
255 
256 	public void setName(String name)
257 	{
258 		m_location.setName(name);
259 	}
260 
261 	public void setOwner(User owner)
262 	{
263 		m_location.setOwner(owner);
264 	}
265 
266 	public void setPhoneNumber(String phoneNumber)
267 	{
268 		m_location.setPhoneNumber(phoneNumber);
269 	}
270 
271 	public void setStoreHours(StoreHours storeHours)
272 	{
273 		m_location.setStoreHours(storeHours);
274 	}
275 
276 	public void setStoreHoursString(String storeHoursString)
277 	{
278 		m_location.setStoreHoursString(storeHoursString);
279 	}
280 
281 	public void setType(LocationType type)
282 	{
283 		m_location.setType(type);
284 	}
285 
286 	public int[] getOpenAmpms()
287 	{
288 		return m_openAmpms;
289 	}
290 
291 	public void setOpenAmpms(int[] openAmpms)
292 	{
293 		m_openAmpms = openAmpms;
294 	}
295 
296 	public int[] getCloseAmpms()
297 	{
298 		return m_closeAmpms;
299 	}
300 
301 	public void setCloseAmpms(int[] closeAmpms)
302 	{
303 		m_closeAmpms = closeAmpms;
304 	}
305 }