641009f6a1d023f51e8d321a1f8299380db4b2ba
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / json / processor / TermBaseBeanProcessor.java
1 // $Id: TaxonBeanProcessor.java 5561 2009-04-07 12:25:33Z a.kohlbecker $
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.remote.json.processor;
12
13 import java.util.Arrays;
14 import java.util.List;
15
16 import net.sf.json.JSONObject;
17 import net.sf.json.JsonConfig;
18
19 import org.apache.log4j.Logger;
20 import org.hibernate.Hibernate;
21
22 import eu.etaxonomy.cdm.model.common.Language;
23 import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
24 import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
25 import eu.etaxonomy.cdm.model.common.Representation;
26 import eu.etaxonomy.cdm.model.common.TermBase;
27 import eu.etaxonomy.cdm.model.common.TermVocabulary;
28 import eu.etaxonomy.cdm.remote.l10n.LocaleContext;
29
30 /**
31 * @author a.kohlbecker
32 *
33 */
34 public class TermBaseBeanProcessor extends AbstractCdmBeanProcessor<TermBase> {
35
36 public static final Logger logger = Logger.getLogger(TermBaseBeanProcessor.class);
37
38 private static final List<String> IGNORE_LIST = Arrays.asList(new String[] {
39 "representations",
40 "inversRepresentations",
41 "terms",
42 "partOf" // exclude partof to avoid lazy loading exceptions
43 });
44
45 private boolean replaceRepresentations = false;
46
47 public boolean isReplaceRepresentations() {
48 return replaceRepresentations;
49 }
50
51 public void setReplaceRepresentations(boolean replace) {
52 this.replaceRepresentations = replace;
53 }
54
55 /* (non-Javadoc)
56 * @see eu.etaxonomy.cdm.remote.json.processor.AbstractCdmBeanProcessor#getIgnorePropNames()
57 */
58 @Override
59 public List<String> getIgnorePropNames() {
60 return IGNORE_LIST;
61 }
62
63 /* (non-Javadoc)
64 * @see eu.etaxonomy.cdm.remote.json.processor.AbstractCdmBeanProcessor#processBeanSecondStep(eu.etaxonomy.cdm.model.common.CdmBase, net.sf.json.JSONObject, net.sf.json.JsonConfig)
65 */
66 @Override
67 public JSONObject processBeanSecondStep(TermBase term, JSONObject json, JsonConfig jsonConfig) {
68
69 // handle OrderedTermVocabulary
70 if(OrderedTermVocabulary.class.isAssignableFrom(term.getClass())){
71 OrderedTermVocabulary otv = (OrderedTermVocabulary)term;
72 if(Hibernate.isInitialized(otv.getTerms())){
73 json.element("terms", otv.getOrderedTerms(), jsonConfig);
74 }
75 } else if(TermVocabulary.class.isAssignableFrom(term.getClass())) {
76 TermVocabulary tv = (TermVocabulary)term;
77 if(Hibernate.isInitialized(tv.getTerms())){
78 json.element("terms", tv.getTerms(), jsonConfig);
79 }
80 }
81
82 List<Language> languages = LocaleContext.getLanguages();
83
84 Representation representation;
85 if(Hibernate.isInitialized(term.getRepresentations())){
86 representation = term.getPreferredRepresentation(languages);
87 if(representation != null){
88 if(representation.getText() != null && representation.getText().length() != 0){
89 json.element("representation_L10n", representation.getText());
90 } else if (representation.getLabel() != null && representation.getLabel().length() !=0) {
91 json.element("representation_L10n", representation.getLabel());
92 } else {
93 json.element("representation_L10n", representation.getAbbreviatedLabel());
94 }
95 }
96 if(!replaceRepresentations){
97 json.element("representations", term.getRepresentations(), jsonConfig);
98 }
99 } else {
100 logger.debug("representations of term not initialized " + term.getUuid().toString());
101 }
102
103 // add additional representation for RelationShipBase
104 if(RelationshipTermBase.class.isAssignableFrom(term.getClass())){
105 RelationshipTermBase<?> relTerm = (RelationshipTermBase<?>)term;
106 Representation inversRepresentation;
107 if(Hibernate.isInitialized(relTerm.getInverseRepresentations())){
108 inversRepresentation = relTerm.getPreferredInverseRepresentation(languages);
109 if(inversRepresentation != null){
110 if(inversRepresentation.getText() != null && inversRepresentation.getText().length() != 0){
111 json.element("inverseRepresentation_L10n", inversRepresentation.getText());
112 } else if (inversRepresentation.getLabel() != null && inversRepresentation.getLabel().length() !=0) {
113 json.element("inverseRepresentation_L10n", inversRepresentation.getLabel());
114 } else {
115 json.element("inverseRepresentation_L10n", inversRepresentation.getAbbreviatedLabel());
116 }
117 }
118 if(!replaceRepresentations){
119 json.element("inverseRepresentations", relTerm.getRepresentations(), jsonConfig);
120 }
121 } else {
122 logger.debug("inverseRepresentations of term not initialized " + relTerm.getUuid().toString());
123 }
124 }
125 return json;
126 }
127
128 }