5772660bf298fabcf905f1228c87414edc7cf3fe
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / json / processor / bean / 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.bean;
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 "infraGenericMarker", //FIXME leads to IllegalStateException if not excluded
43 "partOf" // exclude partOf to avoid lazy loading exceptions,
44 });
45
46 private boolean replaceRepresentations = false;
47
48 public boolean isReplaceRepresentations() {
49 return replaceRepresentations;
50 }
51
52 public void setReplaceRepresentations(boolean replace) {
53 this.replaceRepresentations = replace;
54 }
55
56 /* (non-Javadoc)
57 * @see eu.etaxonomy.cdm.remote.json.processor.AbstractCdmBeanProcessor#getIgnorePropNames()
58 */
59 @Override
60 public List<String> getIgnorePropNames() {
61 return IGNORE_LIST;
62 }
63
64 /* (non-Javadoc)
65 * @see eu.etaxonomy.cdm.remote.json.processor.AbstractCdmBeanProcessor#processBeanSecondStep(eu.etaxonomy.cdm.model.common.CdmBase, net.sf.json.JSONObject, net.sf.json.JsonConfig)
66 */
67 @Override
68 public JSONObject processBeanSecondStep(TermBase term, JSONObject json, JsonConfig jsonConfig) {
69
70 // handle OrderedTermVocabulary
71 if(OrderedTermVocabulary.class.isAssignableFrom(term.getClass())){
72 OrderedTermVocabulary otv = (OrderedTermVocabulary)term;
73 if(Hibernate.isInitialized(otv.getTerms())){
74 json.element("terms", otv.getOrderedTerms(), jsonConfig);
75 }
76 } else if(TermVocabulary.class.isAssignableFrom(term.getClass())) {
77 TermVocabulary tv = (TermVocabulary)term;
78 if(Hibernate.isInitialized(tv.getTerms())){
79 json.element("terms", tv.getTerms(), jsonConfig);
80 }
81 }
82
83 List<Language> languages = LocaleContext.getLanguages();
84
85 Representation representation;
86 if(Hibernate.isInitialized(term.getRepresentations())){
87 representation = term.getPreferredRepresentation(languages);
88 if(representation != null){
89 if(representation.getText() != null && representation.getText().length() != 0){
90 json.element("representation_L10n", representation.getText());
91 } else if (representation.getLabel() != null && representation.getLabel().length() !=0) {
92 json.element("representation_L10n", representation.getLabel());
93 }
94
95 json.element("representation_L10n_abbreviated", representation.getAbbreviatedLabel());
96
97 }
98 if(!replaceRepresentations){
99 json.element("representations", term.getRepresentations(), jsonConfig);
100 }
101 } else {
102 logger.debug("representations of term not initialized " + term.getUuid().toString());
103 }
104
105 // add additional representation for RelationShipBase
106 if(RelationshipTermBase.class.isAssignableFrom(term.getClass())){
107 RelationshipTermBase<?> relTerm = (RelationshipTermBase<?>)term;
108 Representation inversRepresentation;
109 if(Hibernate.isInitialized(relTerm.getInverseRepresentations())){
110 inversRepresentation = relTerm.getPreferredInverseRepresentation(languages);
111 if(inversRepresentation != null){
112 if(inversRepresentation.getText() != null && inversRepresentation.getText().length() != 0){
113 json.element("inverseRepresentation_L10n", inversRepresentation.getText());
114 } else if (inversRepresentation.getLabel() != null && inversRepresentation.getLabel().length() !=0) {
115 json.element("inverseRepresentation_L10n", inversRepresentation.getLabel());
116 } else {
117 json.element("inverseRepresentation_L10n", inversRepresentation.getAbbreviatedLabel());
118 }
119 }
120 if(!replaceRepresentations){
121 json.element("inverseRepresentations", relTerm.getRepresentations(), jsonConfig);
122 }
123 } else {
124 logger.debug("inverseRepresentations of term not initialized " + relTerm.getUuid().toString());
125 }
126 }
127 return json;
128 }
129
130 }