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