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