restoring accidentally removed formatter code
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / event / EditorActionContextFormatter.java
1 /**
2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.vaadin.event;
10
11 import java.util.Collection;
12 import java.util.Stack;
13
14 import org.apache.commons.lang3.StringUtils;
15
16 import com.vaadin.ui.Component;
17
18 import eu.etaxonomy.cdm.model.common.CdmBase;
19 import eu.etaxonomy.cdm.ref.TypedEntityReference;
20 import eu.etaxonomy.cdm.vaadin.event.EditorActionContextFormat.TargetInfoType;
21 import eu.etaxonomy.cdm.vaadin.model.CdmEntityAdapterDTO;
22 import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
23 import eu.etaxonomy.vaadin.util.PropertyIdPath;
24
25 /**
26 * @author a.kohlbecker
27 * @since Oct 29, 2018
28 *
29 */
30 public class EditorActionContextFormatter {
31
32 private static final String NEW = "New";
33
34 private static final String EDIT = "Edit";
35
36 public String format(EditorActionContext cntxt, EditorActionContextFormat format) {
37
38 String className = null;
39 String targetInfo = null;
40 String targetEntityStr = null;
41 String createOrNew = null;
42
43 Object parentEntity = cntxt.getParentEntity();
44
45 if (parentEntity != null) {
46 if (parentEntity instanceof TypedEntityReference) {
47 className = ((TypedEntityReference) cntxt.getParentEntity()).getType().getSimpleName();
48 createOrNew = EDIT;
49 } else if (CdmEntityAdapterDTO.class.isAssignableFrom(parentEntity.getClass())) {
50 CdmBase cdmEntity = ((CdmEntityAdapterDTO) parentEntity).cdmEntity();
51 className = cdmEntity.getClass().getSimpleName();
52 createOrNew = cdmEntity.isPersited() ? EDIT : NEW;
53 } else if (CdmBase.class.isAssignableFrom(parentEntity.getClass())) {
54 CdmBase cdmEntity = ((CdmBase) parentEntity);
55 className = cdmEntity.getClass().getSimpleName();
56 createOrNew = cdmEntity.isPersited() ? EDIT : NEW;
57 } else {
58 className = parentEntity.getClass().getSimpleName();
59 // can not decide for createOrNew in this case
60 }
61 } else {
62 className += "[NULL_CLASS]";
63 }
64
65 if ((format.doTargetInfo || format.doTargetEntity) && cntxt.getParentView() != null && AbstractPopupEditor.class.isAssignableFrom(cntxt.getParentView().getClass())) {
66 // the top element is the cntxt istself!! we need to dig one step deeper to get the previous popup editor
67 // TODO chaining the EditorActionContext would ease find the contexts of parent editors
68 Stack<EditorActionContext> ctxtStack = ((AbstractPopupEditor)cntxt.getParentView()).getEditorActionContext();
69 int parentPopupPos = ctxtStack.size() - 2;
70 if(parentPopupPos > -1){
71 EditorActionContext parentCtx = ctxtStack.get(parentPopupPos);
72 if(format.targetInfoType.equals(TargetInfoType.PROPERTIES)){
73 PropertyIdPath propertyIdPath = parentCtx.getTargetPropertyIdPath();
74 if (propertyIdPath != null) {
75 targetInfo = propertyIdPath.toString();
76 }
77 } else {
78 // TargetInfoType.FIELD_CAPTION
79 if(parentCtx.getTargetField() != null){
80 Component captionComponent = parentCtx.getTargetField();
81 while(captionComponent != null){
82 targetInfo = captionComponent.getCaption();
83 if(targetInfo != null){
84 break;
85 }
86 captionComponent = captionComponent.getParent();
87 }
88 }
89 }
90 }
91 }
92 if(format.doTargetEntity){
93 targetEntityStr = formatTargetEntityString(cntxt.parentEntity, format);
94 }
95
96 // create output
97 String outStr = "";
98
99 if (format.doCreateOrNew && createOrNew != null) {
100 if(format.tagName != null){
101 outStr += "<" + format.tagName + " class=\"operation " + format.classAttributes + "\">" + createOrNew + "</" + format.tagName + ">";
102 } else {
103 outStr += createOrNew;
104 }
105 }
106
107 if (format.doTargetInfo) {
108 if(targetInfo == null && format.classNameForMissingTargetData && className != null){
109 targetInfo = className;
110 }
111 if(targetInfo != null){
112 if(!outStr.isEmpty()){
113 outStr += " ";
114 targetInfo = normalizeTargetInfo(targetInfo);
115 }
116 if(format.tagName != null){
117 outStr += "<" + format.tagName + " class=\"target " + format.classAttributes + "\">" + targetInfo + "</" + format.tagName + ">";
118 } else {
119 outStr += targetInfo;
120 }
121 }
122 }
123 if(format.doTargetEntity && targetEntityStr != null){
124 if(format.tagName != null){
125 outStr += "<" + format.tagName + " class=\"target-entity" + format.classAttributes + "\">" + targetEntityStr + "</" + format.tagName + ">";
126 } else {
127 outStr += " (" + targetEntityStr + ")";
128 }
129 }
130 return outStr;
131 }
132
133 public String format(Collection<EditorActionContext> cntxts, EditorActionContextFormat format) {
134 String outStr = "";
135 for(EditorActionContext ctx : cntxts){
136 if(!outStr.isEmpty()){
137 outStr += " > "; // FIXME allow configuring the separator
138 }
139 outStr += format(ctx, format) ;
140 }
141 return outStr;
142 }
143
144 /**
145 * @param value
146 * @param format
147 * @return
148 */
149 private String formatTargetEntityString(Object value, EditorActionContextFormat format) {
150
151 if(value == null){
152 return "NULL";
153 }
154 String outStr;
155 if(value instanceof CdmBase){
156 outStr = ((CdmBase)value).instanceToString();
157 } else if(value instanceof CdmEntityAdapterDTO) {
158 outStr = value.getClass().getSimpleName() + ": ";
159 outStr += ((CdmEntityAdapterDTO)value).cdmEntity().toString();
160 } else {
161 outStr = value.getClass().getSimpleName() + ": " + value.toString();
162 }
163 return outStr;
164 }
165
166 /**
167 * @param targetInfo
168 * @return
169 */
170 public String normalizeTargetInfo(String targetInfo) {
171 targetInfo = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(targetInfo), " ");
172 targetInfo = targetInfo.toLowerCase();
173 return targetInfo;
174 }
175
176 }