FragmentImpl.java
18.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.cfm.ContentElement
* com.adobe.cq.dam.cfm.ContentFragment
* com.adobe.cq.dam.cfm.ContentFragmentException
* com.adobe.cq.dam.cfm.ContentVariation
* com.adobe.cq.dam.cfm.ElementTemplate
* com.adobe.cq.dam.cfm.FragmentTemplate
* com.adobe.cq.dam.cfm.VariationDef
* com.adobe.cq.dam.cfm.VariationTemplate
* com.adobe.cq.dam.cfm.VersionDef
* com.adobe.cq.dam.cfm.VersionedContent
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Revision
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.version.Version
* javax.jcr.version.VersionManager
* org.apache.commons.io.IOUtils
* org.apache.commons.io.input.ReaderInputStream
* 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
* org.apache.sling.resource.collection.ResourceCollection
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.cfm.impl;
import com.adobe.cq.dam.cfm.ContentElement;
import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.ContentFragmentException;
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.VariationDef;
import com.adobe.cq.dam.cfm.VariationTemplate;
import com.adobe.cq.dam.cfm.VersionDef;
import com.adobe.cq.dam.cfm.VersionedContent;
import com.adobe.cq.dam.cfm.impl.AbstractAssetBasedElement;
import com.adobe.cq.dam.cfm.impl.AssociatedContentException;
import com.adobe.cq.dam.cfm.impl.ElementImpl;
import com.adobe.cq.dam.cfm.impl.ElementTemplateImpl;
import com.adobe.cq.dam.cfm.impl.FragmentTemplateImpl;
import com.adobe.cq.dam.cfm.impl.FragmentWriteException;
import com.adobe.cq.dam.cfm.impl.InvalidFragmentException;
import com.adobe.cq.dam.cfm.impl.InvalidTemplateException;
import com.adobe.cq.dam.cfm.impl.VariationDefImpl;
import com.adobe.cq.dam.cfm.impl.VersionDefImpl;
import com.adobe.cq.dam.cfm.impl.VersioningException;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Revision;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.version.Version;
import javax.jcr.version.VersionManager;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
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;
import org.apache.sling.resource.collection.ResourceCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FragmentImpl
extends AbstractAssetBasedElement
implements ContentFragment {
private static final String METADATA_PATH = "jcr:content/metadata";
private final Logger log;
private String title;
private String description;
private final List<ContentElement> elements;
private final FragmentTemplate template;
private final ResourceCollection associatedContent;
private final Resource associatedContentResource;
public FragmentImpl(Asset asset, ResourceCollection associatedContent, boolean allowInvalidTemplates) throws InvalidFragmentException {
FragmentTemplateImpl template;
super(asset, asset);
this.log = LoggerFactory.getLogger(this.getClass());
this.elements = new ArrayList<ContentElement>(4);
Resource assetResource = (Resource)asset.adaptTo(Resource.class);
if (assetResource == null) {
throw new InvalidFragmentException("Invalid asset, can't be adapted to a Resource.");
}
Resource modelResource = assetResource.getChild("model");
if (modelResource == null) {
throw new InvalidFragmentException("No model definition found.");
}
try {
template = new FragmentTemplateImpl(modelResource);
}
catch (InvalidTemplateException ite) {
throw new InvalidFragmentException("Invalid model for fragment at '" + asset.getPath() + "'", (Throwable)((Object)ite));
}
ValueMap contentProps = (ValueMap)assetResource.getChild("jcr:content").adaptTo(ValueMap.class);
this.title = (String)contentProps.get("jcr:title", (Object)asset.getName());
this.description = (String)contentProps.get("jcr:description", (Object)"");
this.associatedContentResource = assetResource.getChild("associated");
this.template = template;
this.associatedContent = associatedContent;
this.addElement(asset, template.getMasterElement(), allowInvalidTemplates);
Collection subAssets = asset.getSubAssets();
for (Asset subAsset : subAssets) {
this.addElement(subAsset, template.getForAsset(subAsset), allowInvalidTemplates);
}
}
private void precreateVariations(FragmentTemplate template, ContentElement element) throws ContentFragmentException {
Iterator variations = template.getVariations();
while (variations.hasNext()) {
VariationTemplate variation = (VariationTemplate)variations.next();
element.createVariation(variation);
}
}
private boolean isSmartCollection(Resource collection) {
return collection.isResourceType("dam/smartcollection");
}
public Iterator<ContentElement> getElements() {
return this.elements.iterator();
}
public boolean hasElement(String elementName) {
return this.getElement(elementName) != null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public ContentElement createElement(ElementTemplate template) throws ContentFragmentException {
String assetName = template.getName();
Asset asset = this.getAsset();
boolean oldBatchMode = asset.isBatchMode();
asset.setBatchMode(true);
boolean oldSubBatchMode = false;
String content = template.getDefaultContent();
String contentType = template.getInitialContentType();
Asset subAsset = null;
try {
ReaderInputStream is = new ReaderInputStream((Reader)new StringReader(content));
try {
subAsset = asset.addSubAsset(assetName, contentType, (InputStream)is);
}
finally {
IOUtils.closeQuietly((InputStream)is);
}
if (subAsset == null) {
throw new FragmentWriteException("Could not create underlying sub asset.");
}
oldSubBatchMode = subAsset.isBatchMode();
subAsset.setBatchMode(true);
Resource subAssetResource = (Resource)subAsset.adaptTo(Resource.class);
ModifiableValueMap contentProps = (ModifiableValueMap)subAssetResource.getChild("jcr:content").adaptTo(ModifiableValueMap.class);
String title = this.title + " - " + template.getTitle();
contentProps.put((Object)"jcr:title", (Object)title);
try {
ElementImpl element = this.addElement(subAsset, template, false);
((ElementTemplateImpl)template).linkToAsset(subAsset, false);
this.precreateVariations(this.template, element);
ElementImpl elementImpl = element;
return elementImpl;
}
catch (InvalidFragmentException ife) {
throw new FragmentWriteException("Internal error; element written could not be read.", (Throwable)((Object)ife));
}
}
finally {
if (subAsset != null) {
subAsset.setBatchMode(oldSubBatchMode);
}
asset.setBatchMode(oldBatchMode);
}
}
public ContentElement getElement(String elementName) {
if (elementName == null || elementName.length() == 0) {
return this.getMasterElement();
}
for (ContentElement element : this.elements) {
if (!element.getName().equals(elementName)) continue;
return element;
}
return null;
}
public String getName() {
return this.getAsset().getName();
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) throws FragmentWriteException {
Resource assetResource = (Resource)this.getAsset().adaptTo(Resource.class);
Resource contentResource = assetResource.getChild("jcr:content");
ModifiableValueMap values = (ModifiableValueMap)contentResource.adaptTo(ModifiableValueMap.class);
values.put((Object)"jcr:title", (Object)title);
this.title = title;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) throws FragmentWriteException {
Resource assetResource = (Resource)this.getAsset().adaptTo(Resource.class);
Resource contentResource = assetResource.getChild("jcr:content");
ModifiableValueMap values = (ModifiableValueMap)contentResource.adaptTo(ModifiableValueMap.class);
values.put((Object)"jcr:description", (Object)description);
this.description = description;
}
public FragmentTemplate getTemplate() {
return this.template;
}
public Map<String, Object> getMetaData() {
HashMap<String, Object> metaData = new HashMap<String, Object>(4);
Map assetMetaData = this.getAsset().getMetadata();
for (String key : assetMetaData.keySet()) {
if (key.startsWith("dam:") || key.startsWith("dc:")) continue;
metaData.put(key, assetMetaData.get(key));
}
return metaData;
}
public void setMetaData(String name, Object value) throws FragmentWriteException {
Resource asset = (Resource)this.getAsset().adaptTo(Resource.class);
ResourceResolver resolver = asset.getResourceResolver();
try {
Resource metaData = asset.getChild("jcr:content/metadata");
if (metaData == null) {
String[] paths = "jcr:content/metadata".split("/");
Resource parentRsc = asset;
for (String toCreate : paths) {
Resource toCheck = parentRsc.getChild(toCreate);
if (toCheck == null) {
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("jcr:primaryType", "nt:unstructured");
parentRsc = resolver.create(parentRsc, toCreate, properties);
continue;
}
parentRsc = toCheck;
}
metaData = parentRsc;
}
ModifiableValueMap metaDataProps = (ModifiableValueMap)metaData.adaptTo(ModifiableValueMap.class);
metaDataProps.put((Object)name, value);
}
catch (PersistenceException pe) {
throw new FragmentWriteException("Could not write meta data property '" + name + "'.", (Throwable)pe);
}
}
public Iterator<VariationDef> listAllVariations() {
ArrayList<String> varNames = new ArrayList<String>();
ArrayList<VariationDefImpl> availableVariations = new ArrayList<VariationDefImpl>();
for (ContentElement element : this.elements) {
Iterator variations = element.getVariations();
while (variations.hasNext()) {
ContentVariation toCheck = (ContentVariation)variations.next();
String varName = toCheck.getName();
if (varNames.contains(varName)) continue;
varNames.add(varName);
availableVariations.add(new VariationDefImpl(toCheck));
}
}
return availableVariations.iterator();
}
public VariationTemplate createVariation(String name, String title, String description) throws FragmentWriteException {
FragmentTemplateImpl fragmentTemplate = (FragmentTemplateImpl)this.template;
Iterator<VariationTemplate> variations = fragmentTemplate.getVariations();
ArrayList<String> names = new ArrayList<String>();
while (variations.hasNext()) {
VariationTemplate variation = variations.next();
names.add(variation.getName());
}
name = name == null ? JcrUtil.createValidName((String)title) : name;
int index = 0;
String prefix = name + "-";
if ("_".equals(name) || "-".equals(name)) {
prefix = "variation-";
name = prefix + ++index;
}
while (names.contains(name)) {
name = prefix + ++index;
}
VariationTemplate variationModel = fragmentTemplate.createModelFor(name, title, description);
for (ContentElement element : this.elements) {
String content = element.getContent();
String contentType = element.getContentType();
((ElementImpl)element).createVariation(variationModel, content, contentType);
}
return variationModel;
}
public void removeVariation(String name) throws ContentFragmentException {
for (ContentElement element : this.elements) {
ContentVariation variation = element.getVariation(name);
if (variation == null) continue;
element.removeVariation(variation);
}
FragmentTemplateImpl fragmentTemplate = (FragmentTemplateImpl)this.template;
fragmentTemplate.removeModelFor(name);
}
public Iterator<Resource> getAssociatedContent() {
return this.associatedContent.getResources();
}
public void addAssociatedContent(Resource content) throws AssociatedContentException {
try {
ResourceCollection collection = (ResourceCollection)content.adaptTo(ResourceCollection.class);
if (collection != null && !this.isSmartCollection(content)) {
collection.add((Resource)this.getAsset().adaptTo(Resource.class));
}
this.associatedContent.add(content);
}
catch (PersistenceException pe) {
throw new AssociatedContentException("Could not add '" + content.getPath() + "' as associated content.", (Throwable)pe);
}
}
public void removeAssociatedContent(Resource content) throws AssociatedContentException {
try {
ResourceCollection collection = (ResourceCollection)content.adaptTo(ResourceCollection.class);
if (collection != null && !this.isSmartCollection(content)) {
collection.remove((Resource)this.getAsset().adaptTo(Resource.class));
}
this.associatedContent.remove(content);
}
catch (PersistenceException pe) {
throw new AssociatedContentException("Could not add '" + content.getPath() + "' as associated content.", (Throwable)pe);
}
}
public Iterator<VersionDef> listVersions() throws VersioningException {
ArrayList<VersionDefImpl> versions = new ArrayList<VersionDefImpl>();
Asset mainAsset = this.getMainAsset();
try {
Collection revisions = mainAsset.getRevisions(null);
for (Revision rev : revisions) {
versions.add(new VersionDefImpl(rev));
}
}
catch (Exception e) {
throw new VersioningException("Could not retrieve list of revisions.", e);
}
return versions.iterator();
}
public VersionedContent getVersionedContent(VersionDef version) throws ContentFragmentException {
return this.getMasterElement().getVersionedContent(version);
}
public ContentElement getMasterElement() {
return this.elements.size() > 0 ? this.elements.get(0) : null;
}
protected ElementImpl addElement(Asset assetToAdd, ElementTemplate template, boolean allowInvalidTemplate) throws InvalidFragmentException {
Asset asset = this.getAsset();
String subPath = null;
if (assetToAdd != asset) {
String assetPath;
String subAssetPath = assetToAdd.getPath();
if (!subAssetPath.startsWith(assetPath = asset.getPath())) {
throw new IllegalArgumentException("Content elements must be sub elements");
}
int assetLen = assetPath.length();
int startPos = assetPath.charAt(assetLen - 1) == '/' ? assetLen : assetLen + 1;
subPath = subAssetPath.substring(startPos);
}
ElementImpl element = new ElementImpl(new ElementImpl.Config(assetToAdd, this.getAsset(), subPath, template, allowInvalidTemplate), allowInvalidTemplate);
this.elements.add(element);
return element;
}
public VersionDef getVersionFromRef(String ref) throws ContentFragmentException {
if (ref == null) {
return null;
}
Iterator<VersionDef> versions = this.listVersions();
while (versions.hasNext()) {
VersionDef toCheck = versions.next();
if (!ref.equals(toCheck.getIdentifier())) continue;
return toCheck;
}
return null;
}
public void restoreVersion(VersionDef version) throws ContentFragmentException {
if (version == null) {
throw new VersioningException("Missing version definition.");
}
if (!(version instanceof VersionDefImpl)) {
throw new VersioningException("Invalid type of version definition.");
}
Revision revision = ((VersionDefImpl)version).getAssetRevision();
Version jcrVersion = revision.getVersion();
try {
Session session = jcrVersion.getSession();
VersionManager vm = session.getWorkspace().getVersionManager();
vm.restore(jcrVersion, true);
vm.checkout(this.getMainAsset().getPath());
}
catch (RepositoryException re) {
throw new ContentFragmentException("Could not restore version.", (Throwable)re);
}
}
}