1 package org.kuali.ole.describe.bo;
2
3 import org.apache.commons.lang.StringEscapeUtils;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.docstore.common.document.*;
6 import org.kuali.ole.docstore.model.bo.*;
7 import org.kuali.ole.docstore.model.enums.DocType;
8
9 import java.text.CharacterIterator;
10 import java.text.StringCharacterIterator;
11 import java.util.Map;
12
13
14
15
16
17
18
19
20 public class DocumentTreeNode {
21
22 private String title;
23 private String uuid;
24 private boolean select;
25 private boolean returnCheck;
26
27 private OleDocument oleDocument;
28 private Bib bib;
29 private Holdings holdings;
30 private Item item;
31 private BibTree bibTree;
32
33 private String selectedInstance;
34 private Map<String, String> selectionMap;
35 private String holdingLocation;
36
37 public DocumentTreeNode() {
38
39 }
40
41 public BibTree getBibTree() {
42 return bibTree;
43 }
44
45 public void setBibTree(BibTree bibTree) {
46 this.bibTree = bibTree;
47 }
48
49 public Holdings getHoldings() {
50 return holdings;
51 }
52
53 public Item getItem() {
54 return item;
55 }
56
57 public void setItem(Item item) {
58 this.item = item;
59 if (item != null) {
60 setTitle(buildTreeDataForItem(item, item.getHolding()));
61 setUuid(item.getId());
62 } else {
63 setTitle("");
64 }
65 }
66
67 public String getTitle() {
68 return title;
69 }
70
71 public void setTitle(String title) {
72 this.title = title;
73 }
74
75 public String getUuid() {
76 return uuid;
77 }
78
79 public void setUuid(String uuid) {
80 this.uuid = uuid;
81 }
82
83 public void setBib(Bib bib) {
84 this.bib = bib;
85 StringBuilder titleBuilder = new StringBuilder();
86 if (bib != null) {
87 if (bib.getTitle() != null) {
88 titleBuilder.append(bib.getTitle());
89 }
90 if (titleBuilder.length() > 0) {
91 titleBuilder.append(" / ");
92 }
93 if (bib.getAuthor() != null) {
94 titleBuilder.append(bib.getAuthor());
95 }
96 if (bib.isStaffOnly()) {
97
98 setTitle("<i><font color='red'>" + encodeString(titleBuilder.toString()) + "</font></i>");
99 } else {
100
101 setTitle(encodeString(titleBuilder.toString()));
102 }
103 } else {
104 if (bib.isStaffOnly()) {
105 setTitle("<i><font color='red'>" + "Bibliographic Title" + "</font></i>");
106 } else {
107 setTitle("Bibliographic Title");
108 }
109 }
110
111 setUuid(bib.getId());
112
113 }
114
115 protected String encodeString(String label) {
116 StringBuilder result = new StringBuilder();
117 StringCharacterIterator iterator = new StringCharacterIterator(label);
118 char character = iterator.current();
119 while (character != CharacterIterator.DONE) {
120 if (character == '<') {
121 result.append("<");
122 } else if (character == '>') {
123 result.append(">");
124 } else if (character == '&') {
125 result.append("&");
126 } else if (character == '\"') {
127 result.append(""");
128 } else if (character == '\t') {
129 addCharEntity(9, result);
130 } else if (character == '!') {
131 addCharEntity(33, result);
132 } else if (character == '#') {
133 addCharEntity(35, result);
134 } else if (character == '$') {
135 addCharEntity(36, result);
136 } else if (character == '%') {
137 addCharEntity(37, result);
138 } else if (character == '\'') {
139 addCharEntity(39, result);
140 } else if (character == '(') {
141 addCharEntity(40, result);
142 } else if (character == ')') {
143 addCharEntity(41, result);
144 } else if (character == '*') {
145 addCharEntity(42, result);
146 } else if (character == '+') {
147 addCharEntity(43, result);
148 } else if (character == ',') {
149 addCharEntity(44, result);
150 } else if (character == '-') {
151 addCharEntity(45, result);
152 } else if (character == '.') {
153 addCharEntity(46, result);
154 } else if (character == '/') {
155 addCharEntity(47, result);
156 } else if (character == ':') {
157 addCharEntity(58, result);
158 } else if (character == ';') {
159 addCharEntity(59, result);
160 } else if (character == '=') {
161 addCharEntity(61, result);
162 } else if (character == '?') {
163 addCharEntity(63, result);
164 } else if (character == '@') {
165 addCharEntity(64, result);
166 } else if (character == '[') {
167 addCharEntity(91, result);
168 } else if (character == '\\') {
169 addCharEntity(92, result);
170 } else if (character == ']') {
171 addCharEntity(93, result);
172 } else if (character == '^') {
173 addCharEntity(94, result);
174 } else if (character == '_') {
175 addCharEntity(95, result);
176 } else if (character == '`') {
177 addCharEntity(96, result);
178 } else if (character == '{') {
179 addCharEntity(123, result);
180 } else if (character == '|') {
181 addCharEntity(124, result);
182 } else if (character == '}') {
183 addCharEntity(125, result);
184 } else if (character == '~') {
185 addCharEntity(126, result);
186 } else {
187
188
189 result.append(character);
190 }
191 character = iterator.next();
192 }
193 return result.toString();
194 }
195
196 private void addCharEntity(Integer escapeId, StringBuilder labelBuilder) {
197 String padding = "";
198 if (escapeId <= 9) {
199 padding = "00";
200 } else if (escapeId <= 99) {
201 padding = "0";
202 } else {
203
204 }
205 String number = padding + escapeId.toString();
206 labelBuilder.append("&#" + number + ";");
207 }
208
209
210 public void setHoldings(HoldingsTree holdingsTree) {
211 this.holdings = holdingsTree.getHoldings();
212
213 if (holdingsTree != null && holdingsTree.getHoldings() != null) {
214 if (holdingsTree.getHoldings().getHoldingsType().equalsIgnoreCase("electronic")) {
215 setEholdings(holdingsTree);
216 } else {
217 setTitle(buildTreeDataForHoldings(holdingsTree));
218 setUuid(holdingsTree.getHoldings().getId());
219 }
220
221 } else {
222 setTitle("");
223 }
224
225 }
226
227 public void setWorkBibDocument(WorkBibDocument workBibDocument) {
228 this.oleDocument = workBibDocument;
229 StringBuilder titleBuilder = new StringBuilder();
230 if (workBibDocument != null) {
231 if (workBibDocument.getTitle() != null) {
232 titleBuilder.append(workBibDocument.getTitle());
233 }
234 if (titleBuilder.length() > 0) {
235 titleBuilder.append(" / ");
236 }
237 if (workBibDocument.getAuthor() != null) {
238 titleBuilder.append(workBibDocument.getAuthor());
239 }
240
241 setTitle(StringEscapeUtils.escapeHtml(titleBuilder.toString()));
242 } else {
243 setTitle("Bibliographic Title");
244 }
245
246 setUuid(workBibDocument.getId());
247
248 }
249
250 public void setWorkItemDocument(Item item) {
251 this.item = item;
252 if (item != null) {
253 setTitle(buildTreeDataForItem(item));
254 } else {
255 setTitle("Item");
256 }
257
258 setUuid(item.getId());
259 }
260
261 public void setWorkHoldingsDocument(Item item, Holdings holdings) {
262 this.item = item;
263 if (item != null) {
264 setTitle(buildTreeDataForItem(item, holdings));
265 } else {
266 setTitle("Item");
267 }
268
269 setUuid(item.getId());
270 }
271
272 public void setEholdings(HoldingsTree holdingsTree) {
273 this.holdings = holdingsTree.getHoldings();
274 setTitle(buildTreeDataForHoldings(holdingsTree));
275 String eHoldingsTitle = OLEConstants.E_HOLDINGS_DOC_TYPE;
276 StringBuffer stringBuffer = new StringBuffer();
277 if (holdingsTree != null && holdingsTree.getHoldings() != null) {
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 if ( holdingsTree.getHoldings().isStaffOnly()) {
296 String label = getTitle();
297 label = "<i><font color='red'>" + label + "</font></i>";
298 setTitle(label);
299 }
300 }
301
302
303 setUuid( holdingsTree.getHoldings().getId() + " " + DocType.EHOLDINGS.getCode());
304 }
305
306 public OleDocument getWorkBibDocument() {
307 getTitle();
308 getUuid();
309 return oleDocument = new WorkBibDocument();
310 }
311
312 public OleDocument getWorkInstanceDocument() {
313
314
315 return oleDocument = new WorkInstanceDocument();
316 }
317
318 public OleDocument getWorkItemDocument() {
319 getTitle();
320 getUuid();
321 return oleDocument = new WorkItemDocument();
322 }
323
324
325 public OleDocument getWorkEInstanceDocument() {
326 getTitle();
327 getUuid();
328 return oleDocument = new WorkEInstanceDocument();
329 }
330
331
332 public String getSelectedInstance() {
333 return selectedInstance;
334 }
335
336 public void setSelectedInstance(String selectedInstance) {
337 this.selectedInstance = selectedInstance;
338 }
339
340
341 public boolean isSelect() {
342 return select;
343 }
344
345 public void setSelect(boolean select) {
346 this.select = select;
347 }
348
349 public Map<String, String> getSelectionMap() {
350 return selectionMap;
351 }
352
353 public void setSelectionMap(Map<String, String> selectionMap) {
354 this.selectionMap = selectionMap;
355 }
356
357 public boolean isReturnCheck() {
358 return returnCheck;
359 }
360
361 public void setReturnCheck(boolean returnCheck) {
362 this.returnCheck = returnCheck;
363 }
364
365
366 public String buildTreeDataForHoldings(HoldingsTree holdingsTree) {
367
368 return new EditorFormDataHandler().getHoldingsLabel(holdingsTree);
369 }
370
371 public String buildTreeDataForItem(Item item, Holdings holdings) {
372
373 return new EditorFormDataHandler().getItemLabel(holdings, item);
374
375 }
376
377 public String buildTreeDataForItem(Item item) {
378 String itemLevelContent = null;
379 String itemLocation = null;
380 StringBuilder treeBuilder = new StringBuilder();
381 String locationName = this.getHoldingLocation();
382 if (item.getLocation() != null) {
383 itemLocation = item.getLocation();
384 if (!(itemLocation).equalsIgnoreCase(locationName)) {
385
386 treeBuilder.append(itemLocation);
387 }
388 }
389 StringBuffer removeFromCallNumber = new StringBuffer();
390 String callNumberPrefix = null;
391
392 if (item.getLocation() != null) {
393 itemLocation = item.getLocation();
394 if (!(itemLocation).equalsIgnoreCase(locationName)) {
395
396 treeBuilder.append(itemLocation);
397 }
398 }
399 if (item.getCallNumberPrefix() != null && item.getCallNumberPrefix().length() > 0) {
400 callNumberPrefix = item.getCallNumberPrefix();
401 }
402 if (callNumberPrefix != null) {
403 if (treeBuilder.length() > 0) {
404 treeBuilder.append("-").append(callNumberPrefix);
405 } else {
406 treeBuilder.append(callNumberPrefix);
407 }
408 }
409 if (item.getCallNumber() != null && item.getCallNumber().length() > 0) {
410 if (treeBuilder.length() > 0) {
411 treeBuilder.append("-");
412 }
413 treeBuilder.append(item.getCallNumber());
414 }
415 if (item.getEnumeration() != null && item.getEnumeration().length() > 0) {
416 if (treeBuilder.length() > 0) {
417 treeBuilder.append("-");
418 }
419 treeBuilder.append(item.getEnumeration());
420 removeFromCallNumber.append(" " + item.getEnumeration());
421 }
422 if (item.getChronology() != null && item.getChronology().length() > 0) {
423 if (treeBuilder.length() > 0) {
424 treeBuilder.append("-");
425 }
426 treeBuilder.append(item.getChronology());
427 removeFromCallNumber.append(" " + item.getChronology());
428
429 }
430 if (item.getCopyNumber() != null && item.getCopyNumber().length() > 0) {
431 if (treeBuilder.length() > 0) {
432 treeBuilder.append("-");
433 }
434 treeBuilder.append(item.getCopyNumber());
435 removeFromCallNumber.append(" " + item.getCopyNumber());
436 }
437 if (item.getBarcode() != null && item.getBarcode().length() > 0) {
438 if (treeBuilder.length() > 0) {
439 treeBuilder.append("-");
440 }
441 treeBuilder.append(item.getBarcode());
442 }
443 if (treeBuilder.length() == 0) {
444 if (item.getVolumeNumber() != null) {
445 treeBuilder.append(item.getVolumeNumber());
446 } else {
447 treeBuilder.append("Item");
448 }
449 }
450 itemLevelContent = treeBuilder.toString().replaceAll(removeFromCallNumber.toString(), "");
451
452 return itemLevelContent;
453
454 }
455
456 public String getHoldingLocation() {
457 return holdingLocation;
458 }
459
460 public void setHoldingLocation(String holdingLocation) {
461 this.holdingLocation = holdingLocation;
462 }
463 }