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.data.ametysobject.ModifiableModelAwareDataAwareAmetysObject;
033import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
034import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
035import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
036import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
037import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
038import org.ametys.runtime.model.DefaultElementDefinition;
039import org.ametys.runtime.model.Model;
040import org.ametys.runtime.model.exception.BadItemTypeException;
041import org.ametys.runtime.model.exception.UnknownTypeException;
042import org.ametys.runtime.model.type.ModelItemTypeConstants;
043
044/** 
045 * Simple object representing the result of a consistency check.
046 */
047public class ContentConsistencyResult extends SimpleAmetysObject<ContentConsistencyResultFactory> implements ModifiableModelAwareDataAwareAmetysObject
048{
049    /** data path for the server error*/
050    public static final String SERVER_ERROR = "serverError";
051    /** data path for the not found error */
052    public static final String NOT_FOUND = "notFound";
053    /** data path for the unauthorized error */
054    public static final String UNAUTHORIZED = "unauthorized";
055    /** data path for the unknown error */
056    public static final String UNKNOWN = "unknown";
057    /** data path for the success checks */
058    public static final String SUCCESS = "success";
059    /** data path for the date of the check */
060    public static final String DATE = "date";
061    /** data path for the workflow step */
062    public static final String WORKFLOW_STEP = "workflowStep";
063    /** data path for the content types */
064    public static final String CONTENT_TYPES = "contentTypes";
065    /** data path for the title */
066    public static final String TITLE = "title";
067    /** data path for the content id */
068    public static final String CONTENT_ID = "contentId";
069    /** data path for the last contributor */
070    public static final String LAST_CONTRIBUTOR = "contributor";
071    /** data path for the context */
072    public static final String CONTEXT = "context";
073    
074    private Model _model;
075
076    /**
077     * Creates an {@link ContentConsistencyResult}.
078     * @param node the node backing this result
079     * @param parentPath the parentPath in the Ametys hierarchy
080     * @param factory the factory which created the activity
081     */
082    public ContentConsistencyResult(Node node, String parentPath, ContentConsistencyResultFactory factory)
083    {
084        super(node, parentPath, factory);
085    }
086    
087    private Collection< ? extends Model> _getOrCreateModel()
088    {
089        if (_model == null)
090        {
091            try
092            {
093                _model = Model.of("ContentConsistencyResult", "ContentConsistencyResult",
094                        DefaultElementDefinition.of(CONTENT_ID, false, ModelItemTypeConstants.STRING_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
095                        DefaultElementDefinition.of(CONTEXT, false, ModelItemTypeConstants.STRING_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
096                        DefaultElementDefinition.of(TITLE, false, ModelItemTypeConstants.STRING_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
097                        DefaultElementDefinition.of(CONTENT_TYPES, true, ModelItemTypeConstants.STRING_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
098                        DefaultElementDefinition.of(LAST_CONTRIBUTOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
099                        DefaultElementDefinition.of(WORKFLOW_STEP, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
100                        DefaultElementDefinition.of(DATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
101                        DefaultElementDefinition.of(SUCCESS, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
102                        DefaultElementDefinition.of(UNKNOWN, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
103                        DefaultElementDefinition.of(UNAUTHORIZED, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
104                        DefaultElementDefinition.of(NOT_FOUND, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC),
105                        DefaultElementDefinition.of(SERVER_ERROR, false, ModelItemTypeConstants.LONG_TYPE_ID, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC)
106                        );  
107            }
108            catch (UnknownTypeException | BadItemTypeException | ServiceException e)
109            {
110                throw new AmetysRepositoryException("An error occurred while create the content consistency result model", e);
111            }
112        }
113        return Collections.singleton(_model);
114    }
115    
116    /**
117     * Get the content id
118     * @return the content Id
119     */
120    public String getContentId()
121    {
122        return getValue(CONTENT_ID);
123    }
124
125    /**
126     * Get the result date
127     * @return the date
128     */
129    public ZonedDateTime getDate()
130    {
131        return getValue(DATE);
132    }
133
134    /**
135     * Get the unauthorized count
136     * @return the count
137     */
138    public long getUnauthorizedCount()
139    {
140        return getValue(UNAUTHORIZED);
141    }
142
143    /**
144     * Get the unknown count
145     * @return the unknown count
146     */
147    public long getUnknownCount()
148    {
149        return getValue(UNKNOWN);
150    }
151
152    /**
153     * Get the success count
154     * @return the success count
155     */
156    public long getSuccessCount()
157    {
158        return getValue(SUCCESS);
159    }
160
161    /**
162     * Get the not found count
163     * @return the count
164     */
165    public long getNotFoundCount()
166    {
167        return getValue(NOT_FOUND);
168    }
169
170    /**
171     * Get the server error count
172     * @return the count
173     */
174    public long getServerErrorCount()
175    {
176        return getValue(SERVER_ERROR);
177    }
178
179    /**
180     * Get the title
181     * @return the title
182     */
183    public String getTitle()
184    {
185        return getValue(TITLE);
186    }
187
188    /**
189     * Get the content types
190     * @return the content types
191     */
192    public String[] getContentTypes()
193    {
194        return getValue(CONTENT_TYPES);
195    }
196
197    /**
198     * Get the last contributor
199     * @return the last contributor
200     */
201    public UserIdentity getLastContributor()
202    {
203        return getValue(LAST_CONTRIBUTOR);
204    }
205
206    /**
207     * Get the workflow step
208     * @return the workflow step
209     */
210    public long getWorkflowStep()
211    {
212        return getValue(WORKFLOW_STEP);
213    }
214
215    /**
216     * Serialize the result to a JSON object
217     * @return the result as JSON
218     */
219    public Map<String, Object> toJSON()
220    {
221        HashMap<String, Object> json = new HashMap<>();
222        json.put(CONTENT_ID, getContentId());
223        json.put(CONTEXT, getValue(CONTEXT));
224        json.put(DATE, getDate());
225        json.put(SUCCESS, getSuccessCount());
226        json.put(NOT_FOUND, getNotFoundCount());
227        json.put(UNAUTHORIZED, getUnauthorizedCount());
228        json.put(SERVER_ERROR, getServerErrorCount());
229        json.put(UNKNOWN, getUnknownCount());
230        return json;
231    }
232
233    public ModifiableModelAwareDataHolder getDataHolder()
234    {
235        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
236        return new DefaultModifiableModelAwareDataHolder(repositoryData, Optional.empty(), Optional.empty(), _getOrCreateModel());
237    }
238}