001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.repository.content.jcr;
017
018import java.time.ZonedDateTime;
019import java.util.Date;
020import java.util.List;
021import java.util.Locale;
022import java.util.Map;
023
024import javax.jcr.Node;
025
026import org.ametys.cms.content.references.OutgoingReferences;
027import org.ametys.cms.repository.ModifiableContentDataHolder;
028import org.ametys.cms.repository.ModifiableWorkflowAwareContent;
029import org.ametys.cms.repository.WorkflowAwareContentHelper;
030import org.ametys.cms.repository.comment.Comment;
031import org.ametys.cms.repository.comment.CommentableContent;
032import org.ametys.cms.tag.jcr.TaggableAmetysObjectHelper;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.repository.AmetysRepositoryException;
035import org.ametys.plugins.repository.RepositoryIntegrityViolationException;
036import org.ametys.plugins.repository.data.extractor.xml.XMLValuesExtractorAdditionalDataGetter;
037import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
038import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
039import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
040import org.ametys.plugins.repository.jcr.DublinCoreHelper;
041import org.ametys.plugins.repository.lock.LockableAmetysObject;
042import org.ametys.web.repository.content.ModifiableWebContent;
043
044/**
045 * Modifiable version of {@link DefaultWebContent}.
046 * @param <F> the actual type of factory.
047 */
048public class ModifiableDefaultWebContent<F extends ModifiableDefaultWebContentFactory> extends DefaultWebContent<F> implements ModifiableWebContent, ModifiableWorkflowAwareContent, LockableAmetysObject, CommentableContent//, CopiableAmetysObject, JCRTraversableAmetysObject
049{
050    /**
051     * Creates a {@link ModifiableDefaultWebContent}.
052     * @param node the node backing this {@link ModifiableDefaultWebContent}.
053     * @param parentPath the parent path in the Ametys hierarchy.
054     * @param factory the {@link DefaultWebContentFactory} which creates the AmetysObject.
055     */
056    public ModifiableDefaultWebContent(Node node, String parentPath, F factory)
057    {
058        super(node, parentPath, factory);
059    }
060    
061    // Workflow management.
062    
063    @Override
064    public long getWorkflowId() throws AmetysRepositoryException
065    {
066        return WorkflowAwareContentHelper.getWorkflowId(this);
067    }
068    
069    @Override
070    public void setWorkflowId(long workflowId) throws AmetysRepositoryException
071    {
072        WorkflowAwareContentHelper.setWorkflowId(this, workflowId);
073    }
074    
075    @Override
076    public long getCurrentStepId() throws AmetysRepositoryException
077    {
078        return WorkflowAwareContentHelper.getCurrentStepId(this);
079    }
080    
081    @Override
082    public void setCurrentStepId (long stepId) throws AmetysRepositoryException
083    {
084        WorkflowAwareContentHelper.setCurrentStepId(this, stepId);
085    }
086    
087    @Override
088    public ZonedDateTime getProposalDate() throws AmetysRepositoryException
089    {
090        return WorkflowAwareContentHelper.getProposalDate(this);
091    }
092    
093    @Override
094    public void setProposalDate(ZonedDateTime proposalDate) throws AmetysRepositoryException
095    {
096        WorkflowAwareContentHelper.setProposalDate(this, proposalDate);
097    }
098    
099    @Override
100    public void remove() throws AmetysRepositoryException, RepositoryIntegrityViolationException
101    {
102        _getFactory().getSharedContentManager().switchSharedContentReferences(this);
103        super.remove();
104    }
105
106    @Override
107    public void setSiteName(String siteName)
108    {
109        // TODO CMS-9336 this metadata should be stored as internal using data holders
110        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
111        repositoryData.setValue(METADATA_SITE, siteName);
112    }
113    
114    /**
115     * Set the type of this content
116     * @param type the type to set
117     * @throws AmetysRepositoryException if something goes wrong when setting the type property of the content in JCR
118     */
119    public void setType(String type) throws AmetysRepositoryException
120    {
121        _getFactory().getModifiableContentHelper().setType(this, type);
122    }
123    
124    @Override
125    public void setTypes(String[] types) throws AmetysRepositoryException
126    {
127        _getFactory().getModifiableContentHelper().setTypes(this, types);
128    }
129    
130    @Override
131    public void setMixinTypes(String[] mixins) throws AmetysRepositoryException
132    {
133        _getFactory().getModifiableContentHelper().setMixinTypes(this, mixins);
134    }
135    
136    @Override
137    public void setLanguage(String language) throws AmetysRepositoryException
138    {
139        _getFactory().getModifiableContentHelper().setLanguage(this, language);
140    }
141    
142    @Override
143    public void setTitle(String title, Locale locale) throws AmetysRepositoryException
144    {
145        _getFactory().getModifiableContentHelper().setTitle(this, title, locale);
146    }
147    
148    @Override
149    @Deprecated
150    public void setTitle(String title) throws AmetysRepositoryException
151    {
152        _getFactory().getModifiableContentHelper().setTitle(this, title);
153    }
154    
155    public void setCreator(UserIdentity user) throws AmetysRepositoryException
156    {
157        _getFactory().getModifiableContentHelper().setCreator(this, user);
158    }
159    
160    public void setCreationDate(Date creationDate) throws AmetysRepositoryException
161    {
162        _getFactory().getModifiableContentHelper().setCreationDate(this, creationDate);
163    }
164    
165    public void setLastContributor(UserIdentity user)
166    {
167        _getFactory().getModifiableContentHelper().setLastContributor(this, user);
168    }
169    
170    public void setLastModified(Date lastModified) throws AmetysRepositoryException
171    {
172        _getFactory().getModifiableContentHelper().setLastModified(this, lastModified);
173    }
174    
175    @Override
176    public void setFirstValidationDate(Date validationDate) throws AmetysRepositoryException
177    {
178        _getFactory().getModifiableContentHelper().setFirstValidationDate(this, validationDate);
179    }
180    
181    @Override
182    public void setLastValidationDate(Date validationDate) throws AmetysRepositoryException
183    {
184        _getFactory().getModifiableContentHelper().setLastValidationDate(this, validationDate);
185    }
186    
187    @Override
188    public void setLastMajorValidationDate(Date validationDate) throws AmetysRepositoryException
189    {
190        _getFactory().getModifiableContentHelper().setLastMajorValidationDate(this, validationDate);
191    }
192    
193    @Override
194    public void setOutgoingReferences(Map<String, OutgoingReferences> references) throws AmetysRepositoryException
195    {
196        _getFactory().getModifiableContentHelper().setOutgoingReferences(this, references);
197    }
198    
199    // ------------------- Tags management -------------------- //
200    @Override
201    public void tag(String tag) throws AmetysRepositoryException
202    {
203        TaggableAmetysObjectHelper.tag(this, tag);
204    }
205    
206    @Override
207    public void untag(String tag) throws AmetysRepositoryException
208    {
209        TaggableAmetysObjectHelper.untag(this, tag);
210    }
211    
212    // Lock management. //
213    
214    @Override
215    public void lock() throws AmetysRepositoryException
216    {
217        _getFactory().getLockComponent().lock(this);
218    }
219    
220    @Override
221    public void unlock() throws AmetysRepositoryException
222    {
223        _getFactory().getLockComponent().unlock(this);
224    }
225    
226    @Override
227    public boolean isLocked() throws AmetysRepositoryException
228    {
229        return _getFactory().getLockComponent().isLocked(this);
230    }
231    
232    @Override
233    public UserIdentity getLockOwner() throws AmetysRepositoryException
234    {
235        return _getFactory().getLockComponent().getLockOwner(this);
236    }
237        
238    // Dublin Core metadata. //
239    
240    @Override
241    public void setDCTitle(String title) throws AmetysRepositoryException
242    {
243        DublinCoreHelper.setDCTitle(this, title);
244    }
245
246    @Override
247    public void setDCCreator(String creator) throws AmetysRepositoryException
248    {
249        DublinCoreHelper.setDCCreator(this, creator);
250    }
251
252    @Override
253    public void setDCSubject(String[] subject) throws AmetysRepositoryException
254    {
255        DublinCoreHelper.setDCSubject(this, subject);
256    }
257
258    @Override
259    public void setDCDescription(String description) throws AmetysRepositoryException
260    {
261        DublinCoreHelper.setDCDescription(this, description);
262    }
263
264    @Override
265    public void setDCPublisher(String publisher) throws AmetysRepositoryException
266    {
267        DublinCoreHelper.setDCPublisher(this, publisher);
268    }
269
270    @Override
271    public void setDCContributor(String contributor) throws AmetysRepositoryException
272    {
273        DublinCoreHelper.setDCContributor(this, contributor);
274    }
275
276    @Override
277    public void setDCDate(Date date) throws AmetysRepositoryException
278    {
279        DublinCoreHelper.setDCDate(this, date);
280    }
281
282    @Override
283    public void setDCType(String type) throws AmetysRepositoryException
284    {
285        DublinCoreHelper.setDCType(this, type);
286    }
287
288    @Override
289    public void setDCFormat(String format) throws AmetysRepositoryException
290    {
291        DublinCoreHelper.setDCFormat(this, format);
292    }
293
294    @Override
295    public void setDCIdentifier(String identifier) throws AmetysRepositoryException
296    {
297        DublinCoreHelper.setDCIdentifier(this, identifier);
298    }
299
300    @Override
301    public void setDCSource(String source) throws AmetysRepositoryException
302    {
303        DublinCoreHelper.setDCSource(this, source);
304    }
305
306    @Override
307    public void setDCLanguage(String language) throws AmetysRepositoryException
308    {
309        DublinCoreHelper.setDCLanguage(this, language);
310    }
311
312    @Override
313    public void setDCRelation(String relation) throws AmetysRepositoryException
314    {
315        DublinCoreHelper.setDCRelation(this, relation);
316    }
317    
318    @Override
319    public void setDCCoverage(String coverage) throws AmetysRepositoryException
320    {
321        DublinCoreHelper.setDCCoverage(this, coverage);
322    }
323    
324    @Override
325    public void setDCRights(String rights) throws AmetysRepositoryException
326    {
327        DublinCoreHelper.setDCRights(this, rights);
328    }
329    
330    @Override
331    public Comment createComment()
332    {
333        return new Comment(this.getUnversionedDataHolder());
334    }
335    
336    public Comment createComment(String commentId, ZonedDateTime creationDate)
337    {
338        return new Comment(this.getUnversionedDataHolder(), commentId, creationDate);
339    }
340    
341    @Override
342    public Comment getComment(String commentId) throws AmetysRepositoryException
343    {
344        return Comment.getComment(this.getUnversionedDataHolder(), commentId);
345    }
346    
347    @Override
348    public List<Comment> getComments(boolean includeNotValidatedComments, boolean includeValidatedComments) throws AmetysRepositoryException
349    {
350        return Comment.getComments(this.getUnversionedDataHolder(), includeNotValidatedComments, includeValidatedComments);
351    }
352    @Override
353    
354    public ModifiableModelAwareDataHolder getDataHolder()
355    {
356        return new ModifiableContentDataHolder(this, _getFactory().getContentDataHelper(), new JCRRepositoryData(getNode()), _getFactory().getContentHelper().getContentTypes(this));
357    }
358    
359    public void fillContent(org.w3c.dom.Node node, XMLValuesExtractorAdditionalDataGetter additionalDataGetter) throws Exception
360    {
361        _getFactory().getContentExtractor().fillContent(this, node, additionalDataGetter);
362    }
363}