Mercurial > hg > ismi-richfaces
comparison src/main/java/de/mpiwg/itgroup/ismi/util/guiComponents/EntityList.java @ 1:2e911857a759
(none)
author | jurzua |
---|---|
date | Wed, 29 Oct 2014 14:00:28 +0000 |
parents | |
children | 59f26a5ef2b3 |
comparison
equal
deleted
inserted
replaced
0:74df02964906 | 1:2e911857a759 |
---|---|
1 package de.mpiwg.itgroup.ismi.util.guiComponents; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.Collections; | |
5 import java.util.HashMap; | |
6 import java.util.List; | |
7 import java.util.Map; | |
8 | |
9 import javax.faces.event.ActionEvent; | |
10 import javax.faces.event.ValueChangeEvent; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 import org.mpi.openmind.cache.WrapperService; | |
14 import org.mpi.openmind.repository.bo.Attribute; | |
15 import org.mpi.openmind.repository.bo.Entity; | |
16 import org.mpi.openmind.repository.bo.Node; | |
17 | |
18 import de.mpiwg.itgroup.ismi.auxObjects.ListenerObject; | |
19 import de.mpiwg.itgroup.ismi.entry.beans.AbstractISMIBean; | |
20 | |
21 public class EntityList extends AbstractISMIBean{ | |
22 | |
23 private static final long serialVersionUID = -3339006604248018647L; | |
24 | |
25 private static Logger logger = Logger.getLogger(EntityList.class); | |
26 | |
27 private List<Entity> entities = new ArrayList<Entity>(); | |
28 private Map<Long, Boolean> selections = new HashMap<Long, Boolean>(); | |
29 private String title; | |
30 | |
31 //some relations have an attribute date | |
32 private boolean useCalendar; | |
33 private Map<Long, Calendar> calendarMap; | |
34 | |
35 private ListenerObject lo; | |
36 | |
37 private String input; | |
38 private Long id; | |
39 | |
40 private WrapperService otg; | |
41 private String user; | |
42 | |
43 public EntityList(String oc, String attName, String title){ | |
44 this.lo = new ListenerObject(oc, attName); | |
45 this.title = title; | |
46 } | |
47 | |
48 public EntityList(String oc, String attName, boolean useCalendar){ | |
49 this.lo = new ListenerObject(oc, attName); | |
50 this.useCalendar = useCalendar; | |
51 this.calendarMap = new HashMap<Long, Calendar>(); | |
52 } | |
53 | |
54 public EntityList(String oc, String attName, WrapperService otg, String user){ | |
55 this.lo = new ListenerObject(oc, attName); | |
56 this.otg = otg; | |
57 this.user = user; | |
58 } | |
59 | |
60 public EntityList(String oc, String attName, WrapperService otg, String user, boolean useCalendar){ | |
61 this.lo = new ListenerObject(oc, attName); | |
62 this.otg = otg; | |
63 this.user = user; | |
64 this.useCalendar = useCalendar; | |
65 this.calendarMap = new HashMap<Long, Calendar>(); | |
66 } | |
67 | |
68 | |
69 public void listenerAddRole(ActionEvent event){ | |
70 if(otg != null && isUnique(id)){ | |
71 Entity role = otg.getEntityById(id); | |
72 if(role != null){ | |
73 this.add(role); | |
74 } | |
75 } | |
76 } | |
77 | |
78 public void listenerCreate(ActionEvent event){ | |
79 try { | |
80 if(otg != null && isUnique(input)){ | |
81 Entity e = new Entity(Node.TYPE_ABOX, lo.getClassObj(), false); | |
82 e.setOwnValue(input); | |
83 e.addAttribute(new Attribute(this.lo.getAttName(), "text", input)); | |
84 otg.saveEntity(e, this.user); | |
85 this.add(e); | |
86 } | |
87 } catch (Exception e) { | |
88 logger.error(e.getMessage(), e); | |
89 e.printStackTrace(); | |
90 this.printInternalError(e); | |
91 } | |
92 } | |
93 | |
94 public void listenerCreateNoUnique(ActionEvent event){ | |
95 try { | |
96 if(otg != null ){ | |
97 Entity e = new Entity(Node.TYPE_ABOX, lo.getClassObj(), false); | |
98 e.setOwnValue(input); | |
99 e.addAttribute(new Attribute(this.lo.getAttName(), "text", input)); | |
100 otg.saveEntity(e, this.user); | |
101 this.add(e); | |
102 } | |
103 } catch (Exception e) { | |
104 logger.error(e.getMessage(), e); | |
105 e.printStackTrace(); | |
106 this.printInternalError(e); | |
107 } | |
108 } | |
109 | |
110 private boolean isUnique(Long id){ | |
111 for(Entity e : entities){ | |
112 if(e.getId().compareTo(id) == 0){ | |
113 addErrorMsg("This entity was already selected."); | |
114 return false; | |
115 } | |
116 } | |
117 return true; | |
118 } | |
119 | |
120 private boolean isUnique(String input){ | |
121 for(Entity e : entities){ | |
122 if(e.getOwnValue().equals(input)){ | |
123 addErrorMsg("An entity " + lo.getClassObj() + " with name " + input + " exists already."); | |
124 return false; | |
125 } | |
126 } | |
127 return true; | |
128 } | |
129 | |
130 public void listenerChanged(ValueChangeEvent event) { | |
131 this.lo = changeListener(event, lo, this.lo.getClassObj(), this.lo.getAttName()); | |
132 if(lo.entity != null && lo.entity.isPersistent()){ | |
133 lo.statusImage.setStatus(StatusImage.STATUS_OK); | |
134 }else{ | |
135 lo.statusImage.setStatus(StatusImage.STATUS_UNSET); | |
136 } | |
137 } | |
138 | |
139 public void listenerAdd(ActionEvent event){ | |
140 this.add(this.lo.entity); | |
141 } | |
142 | |
143 public void listenerRemoveSelection(ActionEvent event){ | |
144 for(Entity remove : getSelectedEntities()){ | |
145 this.remove(remove.getId()); | |
146 } | |
147 } | |
148 | |
149 public void add(Entity e){ | |
150 if(e != null && e.isPersistent()){ | |
151 if(!selections.containsKey(e.getId())){ | |
152 this.entities.add(e); | |
153 this.selections.put(e.getId(), false); | |
154 Collections.sort(this.entities); | |
155 if(useCalendar){ | |
156 Calendar cal = new Calendar(); | |
157 this.calendarMap.put(e.getId(), cal); | |
158 } | |
159 } | |
160 } | |
161 } | |
162 | |
163 public void add(Entity e, Attribute calendarAtt){ | |
164 if(e != null && e.isPersistent()){ | |
165 if(!selections.containsKey(e.getId())){ | |
166 this.entities.add(e); | |
167 this.selections.put(e.getId(), false); | |
168 Collections.sort(this.entities); | |
169 if(useCalendar){ | |
170 if(calendarAtt == null){ | |
171 this.calendarMap.put(e.getId(), new Calendar()); | |
172 }else{ | |
173 this.calendarMap.put(e.getId(), AbstractISMIBean.updateCalendar(calendarAtt)); | |
174 } | |
175 } | |
176 } | |
177 } | |
178 } | |
179 | |
180 public Calendar getCalendar(Long entId){ | |
181 if(calendarMap != null){ | |
182 return calendarMap.get(entId); | |
183 } | |
184 return null; | |
185 } | |
186 | |
187 public void addList(List<Entity> list){ | |
188 for(Entity e : list){ | |
189 this.add(e); | |
190 } | |
191 } | |
192 | |
193 public void remove(Long id){ | |
194 this.selections.remove(id); | |
195 Entity e = null; | |
196 for(Entity ent : entities){ | |
197 if(ent.getId().intValue() == id){ | |
198 e = ent; | |
199 break; | |
200 } | |
201 } | |
202 this.entities.remove(e); | |
203 } | |
204 | |
205 | |
206 | |
207 private List<Entity> getSelectedEntities(){ | |
208 List<Entity> list = new ArrayList<Entity>(); | |
209 for(Entity e : entities){ | |
210 if(selections.get(e.getId())){ | |
211 list.add(e); | |
212 } | |
213 } | |
214 return list; | |
215 } | |
216 | |
217 | |
218 public List<Entity> getEntities() { | |
219 return entities; | |
220 } | |
221 public void setEntities(List<Entity> entities) { | |
222 this.entities = entities; | |
223 } | |
224 public Map<Long, Boolean> getSelections() { | |
225 return selections; | |
226 } | |
227 | |
228 public void setSelections(Map<Long, Boolean> selections) { | |
229 this.selections = selections; | |
230 } | |
231 | |
232 public ListenerObject getLo() { | |
233 return lo; | |
234 } | |
235 | |
236 public void setLo(ListenerObject lo) { | |
237 this.lo = lo; | |
238 } | |
239 | |
240 public String getInput() { | |
241 return input; | |
242 } | |
243 | |
244 public void setInput(String input) { | |
245 this.input = input; | |
246 } | |
247 | |
248 public Long getId() { | |
249 return id; | |
250 } | |
251 | |
252 public void setId(Long id) { | |
253 this.id = id; | |
254 } | |
255 | |
256 public Map<Long, Calendar> getCalendarMap() { | |
257 return calendarMap; | |
258 } | |
259 | |
260 public void setCalendarMap(Map<Long, Calendar> calendarMap) { | |
261 this.calendarMap = calendarMap; | |
262 } | |
263 | |
264 public boolean isUseCalendar() { | |
265 return useCalendar; | |
266 } | |
267 | |
268 public String getTitle() { | |
269 return title; | |
270 } | |
271 } |