Place.java
7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.model;
import java.awt.Color;
import java.util.Calendar;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import fi.codecrew.moya.utilities.NumericStringComparator;
/**
*
*/
@Entity
@Table(name = "places")
public class Place extends GenericEntity implements Comparable<Place> {
/**
*
*/
private static final long serialVersionUID = -5314851260772328611L;
@Lob
@Column(name = "place_description")
private String description;
@Column(name = "place_name")
private String name;
@Column(name = "map_x")
private int mapX = 0;
@Column(name = "map_y")
private int mapY = 0;
@Column(name = "width")
private int width = 0;
@Column(name = "height")
private int height = 0;
@Column(name = "release_time")
@Temporal(TemporalType.TIMESTAMP)
private Calendar releaseTime;
@Column(name = "place_details")
@Lob
private String details = "";
@Column(name = "place_code")
private String code = "";
@OneToOne(mappedBy = "placeReservation")
private GroupMembership placeReserver;
@Column(name = "buyable", nullable = false)
private boolean buyable = true;
@Column(nullable = false)
private boolean disabled = false;
/**
* Which group has bought the place
*/
@ManyToOne
@JoinColumn(name = "group_id", referencedColumnName = "id")
private PlaceGroup group;
@ManyToOne
@JoinColumn(name = "provided_role_id", referencedColumnName = Role.ID_COLUMN)
private Role providesRole;
@JoinColumn(name = "map_id", referencedColumnName = EventMap.ID_COLUMN, nullable = true)
@ManyToOne(optional = true, cascade = CascadeType.ALL)
private EventMap map;
/**
* Which ticket type is this place sold as
*/
@JoinColumn(name = "products_id", referencedColumnName = Product.ID_COLUMN)
@ManyToOne()
private Product product;
/**
* Who is the current currentUser (mapped with code printed on the place) of
* the place. Used in Vectorama currentUser tracking.
*/
@JoinColumn(name = "current_eventuser_id", referencedColumnName = EventUser.ID_COLUMN)
@ManyToOne
private EventUser currentUser;
public static enum PlaceState {
FREE, DISABLED, LOCKED, TEMP_RESERVED_FORME,
MY_PLACE, RESERVED
}
public PlaceState getState(EventUser user)
{
PlaceState ret = PlaceState.FREE;
if (isDisabled()) {
ret = PlaceState.DISABLED;
} else {
if (!isBuyable()) {
ret = PlaceState.LOCKED;
}
if (isTaken()) {
ret = PlaceState.RESERVED;
// logger.debug("Setting place Reserved {}", p);
}
}
if (user != null) {
if (isReservedFor(user)) {
ret = PlaceState.TEMP_RESERVED_FORME;
} else if (user.equals(getCurrentUser())
|| (getGroup() != null && user.equals(getGroup().getCreator()))
|| (getPlaceReserver() != null && user.equals(getPlaceReserver().getUser()))) {
ret = PlaceState.MY_PLACE;
}
}
return ret;
}
public Place() {
super();
}
public Place(EventMap eventMap) {
super();
this.map = eventMap;
}
public String getDescription() {
return description;
}
public void setDescription(String placeDescription) {
this.description = placeDescription;
}
public String getName() {
return name;
}
public void setName(String placeName) {
this.name = placeName;
}
public Integer getMapX() {
return mapX;
}
public void setMapX(Integer mapX) {
this.mapX = mapX;
}
public Integer getMapY() {
return mapY;
}
public void setMapY(Integer mapY) {
this.mapY = mapY;
}
public String getDetails() {
return details;
}
public void setDetails(String placeDetails) {
this.details = placeDetails;
}
public String getCode() {
return code;
}
public void setCode(String placeCode) {
this.code = placeCode;
}
public PlaceGroup getGroup() {
return group;
}
public void setGroup(PlaceGroup group) {
if (group != null && map != null && !group.getEvent().equals(map.getEvent())) {
throw new RuntimeException("Can not set Group from different Event to Place!");
}
this.group = group;
}
public EventMap getMap() {
return map;
}
public void setMap(EventMap map) {
this.map = map;
}
public Product getProduct() {
return product;
}
public void setProduct(Product productsId) {
this.product = productsId;
}
public EventUser getCurrentUser() {
return currentUser;
}
public void setCurrentUser(EventUser usersId) {
this.currentUser = usersId;
}
public void setPlaceReserver(GroupMembership placeReserver) {
this.placeReserver = placeReserver;
}
public GroupMembership getPlaceReserver() {
return placeReserver;
}
/**
* @return the height
*/
public Integer getHeight() {
return height;
}
/**
* @param height
* the height to set
*/
public void setHeight(Integer height) {
this.height = height;
}
/**
* @return the width
*/
public Integer getWidth() {
return width;
}
/**
* @param width
* the width to set
*/
public void setWidth(Integer width) {
this.width = width;
}
public boolean isTaken() {
return (getReleaseTime() != null || getGroup() != null);
}
/**
*
* @return Is the place reserved ( not bought for user)
*/
public boolean isReservedFor(EventUser u) {
return (u.equals(getCurrentUser()) && getGroup() == null);
}
/**
* Check if the places releasetime has expired and it should be released for
* shopping
*
* @return If the status of thie entity changed and it should be merged.
*/
public boolean checkReleased() {
boolean ret = false;
if (getGroup() == null && getReleaseTime() != null && Calendar.getInstance().after(getReleaseTime())) {
setCurrentUser(null);
setReleaseTime(null);
ret = true;
}
return ret;
}
public void setBuyable(boolean buyable) {
this.buyable = buyable;
}
public boolean isBuyable() {
return buyable;
}
public void setReleaseTime(Calendar releaseTime) {
this.releaseTime = releaseTime;
}
public Calendar getReleaseTime() {
return releaseTime;
}
public void setProvidesRole(Role providesRole) {
this.providesRole = providesRole;
}
public Role getProvidesRole() {
return providesRole;
}
public boolean isDisabled() {
return disabled;
}
public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
/**
* Note: this class has a natural ordering that is inconsistent with equals.
*/
@Override
@Transient
public int compareTo(Place o) {
if (this.equals(o)) {
return 0;
}
// Check null values;
// Null is smaller than non null value.
if (this.getName() == null || o.getName() == null) {
if (this.getName() == null) {
return 1;
}
if (o.getName() == null) {
return -1;
}
// both names are null. Compare IDs
return this.getNonNullId().compareTo(o.getNonNullId());
}
if (this.getName().equals(o.getName())) {
return 0;
}
return NumericStringComparator.numericCompare(this.getName(), o.getName());
}
}