001/*
002 *  Copyright 2023 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.cms.content.consistency;
017
018import java.time.ZonedDateTime;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Optional;
022
023import javax.jcr.Node;
024
025import org.apache.avalon.framework.service.ServiceException;
026
027import org.ametys.cms.data.holder.impl.DefaultModifiableModelAwareDataHolder;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.RepositoryConstants;
031import org.ametys.plugins.repository.data.ametysobject.ModifiableModelAwareDataAwareAmetysObject;
032import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
033import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
034import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
035import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
036import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
037import org.ametys.runtime.model.DefaultElementDefinition;
038import org.ametys.runtime.model.Model;
039import org.ametys.runtime.model.exception.BadItemTypeException;
040import org.ametys.runtime.model.exception.UnknownTypeException;
041import org.ametys.runtime.model.type.ModelItemTypeConstants;
042
043/** 
044 * Simple object representing the result of a consistency check.
045 */
046public class ContentConsistencyResult extends SimpleAmetysObject<ContentConsistencyResultFactory> implements ModifiableModelAwareDataAwareAmetysObject
047{
048    /** data path for the server error*/
049    public static final String SERVER_ERROR = "serverError";
050    /** data path for the not found error */
051    public static final String NOT_FOUND = "notFound";
052    /** data path for the unauthorized error */
053    public static final String UNAUTHORIZED = "unauthorized";
054    /** data path for the unknown error */
055    public static final String UNKNOWN = "unknown";
056    /** data path for the success checks */
057    public static final String SUCCESS = "success";
058    /** data path for the date of the check */
059    public static final String DATE = "date";
060    /** data path for the workflow step */
061    public static final String WORKFLOW_STEP = "workflowStep";
062    /** data path for the content types */
063    public static final String CONTENT_TYPES = "contentTypes";
064    /** data path for the title */
065    public static final String TITLE = "title";
066    /** data path for the content id */
067    public static final String CONTENT_ID = "contentId";
068    /** data path for the creator */
069    public static final String CREATOR = "creator";
070    /** data path for the last major validator */
071    public static final String LAST_MAJOR_VALIDATOR = "lastMajorValidator";
072    /** data path for the last validator */
073    public static final String LAST_VALIDATOR = "lastValidator";
074    /** data path for the last contributor */
075    public static final String LAST_CONTRIBUTOR = "contributor";
076    /** data path for the context */
077    public static final String CONTEXT = "context";
078    /** data path for the creation date */
079    public static final String CREATION_DATE = "creationDate";
080    /** data path for the content last major validation date */
081    public static final String LAST_MAJOR_VALIDATION_DATE = "lastMajorValidationDate";
082    /** data path for the content last validation date */
083    public static final String LAST_VALIDATION_DATE = "lastValidationDate";
084    /** data path for the content last modification date */
085    public static final String LAST_MODIFICATION_DATE = "lastModified";
086    
087    private Model _model;
088
089    /** JCR node type for content consistency result */
090    public static final String CONTENT_CONSISTENCY_RESULT_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":consistencyResult";
091    
092    /**
093     * Creates an {@link ContentConsistencyResult}.
094     * @param node the node backing this result
095     * @param parentPath the parentPath in the Ametys hierarchy
096     * @param factory the factory which created the activity
097     */
098    public ContentConsistencyResult(Node node, String parentPath, ContentConsistencyResultFactory factory)
099    {
100        super(node, parentPath, factory);
101    }
102    
103    private Collection< ? extends Model> _getOrCreateModel()
104    {
105        if (_model == null)
106        {
107            try
108            {
109                _model = Model.of("ContentConsistencyResult", "ContentConsistencyResult",
110                        DefaultElementDefinition.of(CONTENT_ID, false, ModelItemTypeConstants.STRING_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
111                        DefaultElementDefinition.of(CONTEXT, false, ModelItemTypeConstants.STRING_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
112                        DefaultElementDefinition.of(TITLE, false, ModelItemTypeConstants.STRING_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
113                        DefaultElementDefinition.of(CONTENT_TYPES, true, ModelItemTypeConstants.STRING_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
114                        DefaultElementDefinition.of(CREATOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
115                        DefaultElementDefinition.of(CREATION_DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
116                        DefaultElementDefinition.of(LAST_MAJOR_VALIDATOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
117                        DefaultElementDefinition.of(LAST_MAJOR_VALIDATION_DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
118                        DefaultElementDefinition.of(LAST_VALIDATOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
119                        DefaultElementDefinition.of(LAST_VALIDATION_DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
120                        DefaultElementDefinition.of(LAST_CONTRIBUTOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
121                        DefaultElementDefinition.of(LAST_MODIFICATION_DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
122                        DefaultElementDefinition.of(WORKFLOW_STEP, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
123                        DefaultElementDefinition.of(DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
124                        DefaultElementDefinition.of(SUCCESS, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
125                        DefaultElementDefinition.of(UNKNOWN, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
126                        DefaultElementDefinition.of(UNAUTHORIZED, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
127                        DefaultElementDefinition.of(NOT_FOUND, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
128                        DefaultElementDefinition.of(SERVER_ERROR, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC)
129                        );  
130            }
131            catch (UnknownTypeException | BadItemTypeException | ServiceException e)
132            {
133                throw new AmetysRepositoryException("An error occurred while create the content consistency result model", e);
134            }
135        }
136        return Collections.singleton(_model);
137    }
138    
139    /**
140     * Get the content id
141     * @return the content Id
142     */
143    public String getContentId()
144    {
145        return getValue(CONTENT_ID);
146    }
147
148    /**
149     * Get the result date
150     * @return the date
151     */
152    public ZonedDateTime getDate()
153    {
154        return getValue(DATE);
155    }
156
157    /**
158     * Get the unauthorized count
159     * @return the count
160     */
161    public long getUnauthorizedCount()
162    {
163        return getValue(UNAUTHORIZED);
164    }
165
166    /**
167     * Get the unknown count
168     * @return the unknown count
169     */
170    public long getUnknownCount()
171    {
172        return getValue(UNKNOWN);
173    }
174
175    /**
176     * Get the success count
177     * @return the success count
178     */
179    public long getSuccessCount()
180    {
181        return getValue(SUCCESS);
182    }
183
184    /**
185     * Get the not found count
186     * @return the count
187     */
188    public long getNotFoundCount()
189    {
190        return getValue(NOT_FOUND);
191    }
192
193    /**
194     * Get the server error count
195     * @return the count
196     */
197    public long getServerErrorCount()
198    {
199        return getValue(SERVER_ERROR);
200    }
201
202    /**
203     * Get the title
204     * @return the title
205     */
206    public String getTitle()
207    {
208        return getValue(TITLE);
209    }
210
211    /**
212     * Get the content types
213     * @return the content types
214     */
215    public String[] getContentTypes()
216    {
217        return getValue(CONTENT_TYPES);
218    }
219
220    /**
221     * Get the creator
222     * @return the creator
223     */
224    public UserIdentity getCreator()
225    {
226        return getValue(CREATOR);
227    }
228    
229    /**
230     * Get the creation date
231     * @return the content creation date
232     */
233    public ZonedDateTime getCreationDate()
234    {
235        return getValue(CREATION_DATE);
236    }
237
238    /**
239     * Get the last major validator
240     * @return the last major validator
241     */
242    public Optional<UserIdentity> getLastMajorValidator()
243    {
244        return Optional.ofNullable(getValue(LAST_MAJOR_VALIDATOR));
245    }
246    
247    /**
248     * Get the last major validation date
249     * @return the last content major validation date
250     */
251    public ZonedDateTime getLastMajorValidationDate()
252    {
253        return getValue(LAST_MAJOR_VALIDATION_DATE);
254    }
255
256    /**
257     * Get the last validator
258     * @return the last validator
259     */
260    public Optional<UserIdentity> getLastValidator()
261    {
262        return Optional.ofNullable(getValue(LAST_VALIDATOR));
263    }
264    
265    /**
266     * Get the last validation date
267     * @return the last content validation date
268     */
269    public ZonedDateTime getLastValidationDate()
270    {
271        return getValue(LAST_VALIDATION_DATE);
272    }
273    
274    /**
275     * Get the last contributor
276     * @return the last contributor
277     */
278    public UserIdentity getLastContributor()
279    {
280        return getValue(LAST_CONTRIBUTOR);
281    }
282    
283    /**
284     * Get the last modification date
285     * @return the content last modification date
286     */
287    public ZonedDateTime getLastModificationDate()
288    {
289        return getValue(LAST_MODIFICATION_DATE);
290    }
291    
292    /**
293     * Get the workflow step
294     * @return the workflow step
295     */
296    public long getWorkflowStep()
297    {
298        return getValue(WORKFLOW_STEP);
299    }
300
301    public ModifiableModelAwareDataHolder getDataHolder()
302    {
303        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
304        return new DefaultModifiableModelAwareDataHolder(repositoryData, Optional.empty(), Optional.empty(), _getOrCreateModel());
305    }
306}