FragmentTemplateImpl.java
11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.cfm.ContentElement
* com.adobe.cq.dam.cfm.ContentVariation
* com.adobe.cq.dam.cfm.ElementTemplate
* com.adobe.cq.dam.cfm.FragmentTemplate
* com.adobe.cq.dam.cfm.MetaDataDefinition
* com.adobe.cq.dam.cfm.VariationTemplate
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.dam.api.Asset
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.cq.dam.cfm.impl;
import com.adobe.cq.dam.cfm.ContentElement;
import com.adobe.cq.dam.cfm.ContentVariation;
import com.adobe.cq.dam.cfm.ElementTemplate;
import com.adobe.cq.dam.cfm.FragmentTemplate;
import com.adobe.cq.dam.cfm.MetaDataDefinition;
import com.adobe.cq.dam.cfm.VariationTemplate;
import com.adobe.cq.dam.cfm.impl.ElementTemplateImpl;
import com.adobe.cq.dam.cfm.impl.FragmentWriteException;
import com.adobe.cq.dam.cfm.impl.InvalidTemplateException;
import com.adobe.cq.dam.cfm.impl.MetaDataDefinitionImpl;
import com.adobe.cq.dam.cfm.impl.VariationTemplateImpl;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.dam.api.Asset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
public class FragmentTemplateImpl
implements FragmentTemplate {
private final Resource resource;
private final String title;
private final String description;
private final List<ElementTemplate> elements;
private final List<VariationTemplate> variations;
private final boolean precreateElements;
private final List<String> initialAssociatedContent;
private final MetaDataDefinition metaData;
public FragmentTemplateImpl(Resource resource) throws InvalidTemplateException {
this.resource = resource;
ValueMap values = (ValueMap)resource.adaptTo(ValueMap.class);
Long version = (Long)values.get("version", Long.class);
if (version == null) {
throw new InvalidTemplateException("Invalid template format; missing 'version' attribute.");
}
if (version > 1) {
throw new InvalidTemplateException("Unsupported template version: " + version);
}
this.title = (String)values.get("jcr:title", (Object)resource.getName());
this.description = (String)values.get("jcr:description", (Object)"");
this.elements = new ArrayList<ElementTemplate>(4);
Resource elements = resource.getChild("elements");
if (elements == null) {
throw new InvalidTemplateException("Template missing element definition.");
}
Iterator elementDefs = elements.listChildren();
if (!elementDefs.hasNext()) {
throw new InvalidTemplateException("Template requires at least one element definition.");
}
this.variations = new ArrayList<VariationTemplate>(4);
Resource variations = resource.getChild("variations");
if (variations != null) {
Iterator variationDefs = variations.listChildren();
while (variationDefs.hasNext()) {
this.variations.add(new VariationTemplateImpl((Resource)variationDefs.next()));
}
}
this.precreateElements = (Boolean)values.get("precreateElements", (Object)false);
this.initialAssociatedContent = new ArrayList<String>();
String[] initialAssociatedContent = (String[])values.get("initialAssociatedContent", String[].class);
if (initialAssociatedContent != null) {
Collections.addAll(this.initialAssociatedContent, initialAssociatedContent);
}
while (elementDefs.hasNext()) {
this.elements.add(new ElementTemplateImpl(this, (Resource)elementDefs.next()));
}
this.metaData = new MetaDataDefinitionImpl(resource.getChild("metadata"));
}
public String getTitle() {
return this.title;
}
public String getDescription() {
return this.description;
}
public Iterator<ElementTemplate> getElements() {
return this.elements.iterator();
}
public ElementTemplate getForElement(ContentElement element) {
String elementName = element.getName();
for (ElementTemplate toCheck : this.elements) {
if (!elementName.equals(toCheck.getName())) continue;
return toCheck;
}
return null;
}
public ElementTemplateImpl getMasterElement() {
if (this.elements.size() < 1) {
throw new IllegalStateException("No element templates available.");
}
return (ElementTemplateImpl)this.elements.get(0);
}
public Iterator<VariationTemplate> getVariations() {
return this.variations.iterator();
}
public VariationTemplate getForVariation(ContentVariation variation) {
String variationName = variation.getName();
for (VariationTemplate toCheck : this.variations) {
if (!variationName.equals(toCheck.getName())) continue;
return toCheck;
}
return null;
}
public VariationTemplate getVariation(String variationName) {
for (VariationTemplate toCheck : this.variations) {
if (!toCheck.getName().equals(variationName)) continue;
return toCheck;
}
return null;
}
public Iterator<String> getInitialAssociatedContent() {
return this.initialAssociatedContent.iterator();
}
public MetaDataDefinition getMetaDataDefinition() {
return this.metaData;
}
private void copy(Resource src, Resource dest) throws PersistenceException {
ResourceResolver resolver = dest.getResourceResolver();
ValueMap srcProps = (ValueMap)src.adaptTo(ValueMap.class);
ModifiableValueMap destProps = (ModifiableValueMap)dest.adaptTo(ModifiableValueMap.class);
Set keys = srcProps.keySet();
for (String key : keys) {
if ("jcr:primaryType".equals(key)) continue;
destProps.put((Object)key, srcProps.get((Object)key));
}
Iterator children = src.listChildren();
while (children.hasNext()) {
Resource child = (Resource)children.next();
HashMap<String, Object> primaryTypeDef = new HashMap<String, Object>(4);
primaryTypeDef.put("jcr:primaryType", ((ValueMap)child.adaptTo(ValueMap.class)).get((Object)"jcr:primaryType"));
Resource childCopy = resolver.create(dest, child.getName(), primaryTypeDef);
this.copy(child, childCopy);
}
}
protected void copyTo(Resource target) throws FragmentWriteException {
ResourceResolver resolver = this.resource.getResourceResolver();
Resource modelResource = target.getChild("model");
try {
if (modelResource != null) {
Iterator children = modelResource.listChildren();
while (children.hasNext()) {
resolver.delete((Resource)children.next());
}
ModifiableValueMap properties = (ModifiableValueMap)modelResource.adaptTo(ModifiableValueMap.class);
properties.clear();
} else {
HashMap<String, String> primaryTypeDef = new HashMap<String, String>(4);
primaryTypeDef.put("jcr:primaryType", "nt:unstructured");
modelResource = resolver.create(target, "model", primaryTypeDef);
}
this.copy(this.resource, modelResource);
}
catch (PersistenceException pe) {
throw new FragmentWriteException("Could not copy template to '" + target.getPath() + "'.", (Throwable)pe);
}
}
public VariationTemplate createModelFor(String name, String title, String description) throws FragmentWriteException {
VariationTemplateImpl theModel;
name = name == null ? JcrUtil.createValidName((String)title) : name;
ResourceResolver resolver = this.resource.getResourceResolver();
try {
Resource varsRsc = this.resource.getChild("variations");
if (varsRsc == null) {
HashMap<String, String> varsProps = new HashMap<String, String>();
varsProps.put("jcr:primaryType", "nt:unstructured");
varsRsc = resolver.create(this.resource, "variations", varsProps);
}
HashMap<String, String> tplProps = new HashMap<String, String>();
tplProps.put("jcr:primaryType", "nt:unstructured");
tplProps.put("name", name);
tplProps.put("jcr:title", title);
tplProps.put("jcr:description", description);
Resource varTplRsc = resolver.create(varsRsc, name, tplProps);
theModel = new VariationTemplateImpl(varTplRsc);
}
catch (PersistenceException pe) {
throw new FragmentWriteException("Could not create variant template '" + name + "'.", (Throwable)pe);
}
catch (InvalidTemplateException ite) {
throw new FragmentWriteException("Could not create variant template '" + name + "'.", (Throwable)((Object)ite));
}
return theModel;
}
public void removeModelFor(String variationName) throws FragmentWriteException {
Resource varsRsc = this.resource.getChild("variations");
if (varsRsc == null) {
throw new FragmentWriteException("No definition for variations found.");
}
Iterator variations = varsRsc.listChildren();
Resource variationToRemove = null;
while (variations.hasNext() && variationToRemove == null) {
Resource variationToCheck = (Resource)variations.next();
ValueMap props = variationToCheck.getValueMap();
String name = (String)props.get("name", String.class);
if (!variationName.equals(name)) continue;
variationToRemove = variationToCheck;
}
if (variationToRemove == null) {
throw new FragmentWriteException("Invalid variation: " + variationName);
}
ResourceResolver resolver = variationToRemove.getResourceResolver();
try {
resolver.delete(variationToRemove);
}
catch (PersistenceException pe) {
throw new FragmentWriteException("Could not delete '" + variationToRemove.getPath() + "'.");
}
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type == Resource.class) {
return (AdapterType)this.resource;
}
return null;
}
public boolean precreateElements() {
return this.precreateElements;
}
public ElementTemplateImpl getForAsset(Asset asset) {
String assetName = asset.getName();
for (ElementTemplate toCheck : this.elements) {
ElementTemplateImpl tplToCheck = (ElementTemplateImpl)toCheck;
if (!assetName.equals(tplToCheck.getAssetName())) continue;
return tplToCheck;
}
return null;
}
}