FragmentManagerImpl.java
10.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
/*
* 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.ContentFragmentManager
* com.adobe.cq.dam.cfm.ContentVariation
* com.adobe.cq.dam.cfm.ElementTemplate
* com.adobe.cq.dam.cfm.FragmentTemplate
* com.adobe.cq.dam.cfm.VariationTemplate
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.AssetManager
* org.apache.commons.io.IOUtils
* org.apache.commons.io.input.ReaderInputStream
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* 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.apache.sling.resource.collection.ResourceCollectionManager
* 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.ContentFragmentManager;
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.VariationTemplate;
import com.adobe.cq.dam.cfm.impl.CFMUtils;
import com.adobe.cq.dam.cfm.impl.ElementTemplateImpl;
import com.adobe.cq.dam.cfm.impl.FragmentImpl;
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.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
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 org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
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.apache.sling.resource.collection.ResourceCollectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
@Component(metatype=0)
public class FragmentManagerImpl
implements ContentFragmentManager {
private final Logger log;
@Reference
private ResourceCollectionManager collectionManager;
public FragmentManagerImpl() {
this.log = LoggerFactory.getLogger(this.getClass());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public ContentFragment create(Resource parent, Resource template, String name, String title) throws ContentFragmentException {
FragmentImpl fragment;
ResourceResolver resolver = parent.getResourceResolver();
Asset asset = null;
boolean oldBatchMode = false;
try {
FragmentTemplateImpl fragmentTemplate;
ResourceCollection associatedContent;
try {
fragmentTemplate = new FragmentTemplateImpl(template);
}
catch (InvalidTemplateException ite) {
throw new FragmentWriteException("Provided template is invalid.", (Throwable)((Object)ite));
}
AssetManager assetManager = (AssetManager)resolver.adaptTo(AssetManager.class);
if (assetManager == null) {
throw new FragmentWriteException("No asset manager available.");
}
String assetPath = null;
int suffix = 0;
while (assetPath == null) {
String toCheck = parent.getPath() + "/" + name + (suffix > 0 ? new StringBuilder().append("-").append(Integer.toString(suffix)).toString() : "");
++suffix;
if (resolver.getResource(toCheck) != null) continue;
assetPath = toCheck;
}
ElementTemplateImpl masterTpl = fragmentTemplate.getMasterElement();
String content = masterTpl.getDefaultContent();
String contentType = masterTpl.getInitialContentType();
ReaderInputStream is = new ReaderInputStream((Reader)new StringReader(content));
try {
asset = assetManager.createAsset(assetPath, (InputStream)is, contentType, false);
}
finally {
IOUtils.closeQuietly((InputStream)is);
}
if (asset == null) {
throw new FragmentWriteException("Could not create underlying asset.");
}
oldBatchMode = asset.isBatchMode();
asset.setBatchMode(true);
Resource assetResource = (Resource)asset.adaptTo(Resource.class);
ModifiableValueMap contentProps = (ModifiableValueMap)assetResource.getChild("jcr:content").adaptTo(ModifiableValueMap.class);
contentProps.put((Object)"contentFragment", (Object)true);
contentProps.put((Object)"jcr:title", (Object)title);
try {
associatedContent = CFMUtils.createEmptyAssocContent(assetResource, this.collectionManager);
Iterator<String> initialAssocContent = fragmentTemplate.getInitialAssociatedContent();
while (initialAssocContent.hasNext()) {
String path = initialAssocContent.next();
Resource contentRsc = resolver.getResource(path);
associatedContent.add(contentRsc);
}
fragmentTemplate.copyTo(assetResource);
}
catch (PersistenceException pe) {
throw new FragmentWriteException("Could not add collection to fragment.", (Throwable)pe);
}
try {
fragment = new FragmentImpl(asset, associatedContent, true);
fragmentTemplate = (FragmentTemplateImpl)fragment.getTemplate();
masterTpl = fragmentTemplate.getMasterElement();
masterTpl.linkToAsset(asset, true);
ContentElement master = fragment.getMasterElement();
Iterator<VariationTemplate> variations = fragmentTemplate.getVariations();
while (variations.hasNext()) {
VariationTemplate variation = variations.next();
master.createVariation(variation);
}
}
catch (InvalidFragmentException ife) {
throw new FragmentWriteException("Could not read the newly created fragment.", (Throwable)((Object)ife));
}
try {
Iterator<ElementTemplate> elements;
fragment = new FragmentImpl(asset, associatedContent, false);
fragmentTemplate = (FragmentTemplateImpl)fragment.getTemplate();
if (fragmentTemplate.precreateElements() && (elements = fragmentTemplate.getElements()).hasNext()) {
elements.next();
while (elements.hasNext()) {
fragment.createElement(elements.next());
}
}
}
catch (InvalidFragmentException ife) {
throw new FragmentWriteException("Could not read the newly created fragment.", (Throwable)((Object)ife));
}
}
finally {
if (asset != null) {
asset.setBatchMode(oldBatchMode);
}
}
return fragment;
}
public Map<Resource, List<Resource>> resolveAssociatedContent(ContentFragment fragment) {
HashMap<Resource, List<Resource>> resolvedAssociatedContent = new HashMap<Resource, List<Resource>>();
Iterator associatedContent = fragment.getAssociatedContent();
while (associatedContent.hasNext()) {
String nodeType;
ValueMap contentProps;
Resource contentResource = (Resource)associatedContent.next();
ArrayList resourcesPerColl = new ArrayList(2);
resolvedAssociatedContent.put(contentResource, resourcesPerColl);
if (contentResource.isResourceType("sling/collection")) {
ResourceCollection coll = this.collectionManager.getCollection(contentResource);
if (coll == null) continue;
Iterator collResources = coll.getResources();
while (collResources.hasNext()) {
resourcesPerColl.add(collResources.next());
}
continue;
}
if (contentResource.getName().equals("jcr:content")) {
contentResource = contentResource.getParent();
}
if (!"dam:Asset".equals(nodeType = (String)(contentProps = (ValueMap)contentResource.adaptTo(ValueMap.class)).get("jcr:primaryType", String.class))) continue;
resourcesPerColl.add((Resource)contentResource);
}
return resolvedAssociatedContent;
}
public List<Resource> resolveAssociatedContentFlat(ContentFragment fragment) {
ArrayList<Resource> resolvedContentFlat = new ArrayList<Resource>();
Map<Resource, List<Resource>> resolvedContent = this.resolveAssociatedContent(fragment);
for (Resource key : resolvedContent.keySet()) {
resolvedContentFlat.addAll((Collection)resolvedContent.get((Object)key));
}
return resolvedContentFlat;
}
protected void bindCollectionManager(ResourceCollectionManager resourceCollectionManager) {
this.collectionManager = resourceCollectionManager;
}
protected void unbindCollectionManager(ResourceCollectionManager resourceCollectionManager) {
if (this.collectionManager == resourceCollectionManager) {
this.collectionManager = null;
}
}
}