001/*
002 *  Copyright 2022 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.plugins.repository.activities;
017
018import java.time.ZonedDateTime;
019import java.util.HashMap;
020import java.util.Map;
021
022import javax.jcr.Node;
023
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.cocoon.xml.XMLUtils;
026import org.xml.sax.ContentHandler;
027import org.xml.sax.SAXException;
028
029import org.ametys.core.user.UserIdentity;
030import org.ametys.plugins.repository.data.ametysobject.ModifiableModelLessDataAwareAmetysObject;
031import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
032import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelLessDataHolder;
033import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
034import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
035import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
036import org.ametys.runtime.model.type.DataContext;
037import org.ametys.runtime.model.type.ModelItemTypeConstants;
038
039/**
040 * Object representing an activity (such as the creation of content)
041 */
042public class Activity extends SimpleAmetysObject<ActivityFactory> implements ModifiableModelLessDataAwareAmetysObject
043{
044    /** key used to store the activity id when serialized */
045    public static final String ACTIVITY_ID_KEY = "id";
046    /** key used to store the activity name when serialized */
047    public static final String ACTIVITY_NAME_KEY = "name";
048
049    /**
050     * Creates an {@link Activity}.
051     * @param node the node backing this {@link Activity}
052     * @param parentPath the parentPath in the Ametys hierarchy
053     * @param factory the factory which created the activity
054     */
055    public Activity(Node node, String parentPath, ActivityFactory factory)
056    {
057        super(node, parentPath, factory);
058    }
059
060    public ModifiableModelLessDataHolder getDataHolder()
061    {
062        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
063        return new DefaultModifiableModelLessDataHolder(_getFactory().getElementTypesExtensionPoint(), repositoryData);
064    }
065    
066    /**
067     * Get the type of the activity from the event type stored in the activity
068     * @return the activity type
069     */
070    public ActivityType getActivityType()
071    {
072        return _getFactory().getActivityTypeFromId(getValue(ActivityFactory.TYPE));
073    }
074    
075    /**
076     * Get the event type that generated this activity
077     * @return the event id
078     */
079    public String getEventType()
080    {
081        return getValueOfType(ActivityFactory.TYPE, ModelItemTypeConstants.STRING_TYPE_ID);
082    }
083    
084    /**
085     * Get the date of the activity
086     * @return the date time
087     */
088    public ZonedDateTime getDate()
089    {
090        return getValueOfType(ActivityFactory.DATE, ModelItemTypeConstants.DATETIME_TYPE_ID);
091    }
092    
093    /**
094     * Get the author of the activity
095     * @return the author identity
096     */
097    public UserIdentity getAuthor()
098    {
099        // FIXME REPOSITORY-504 user elementType is not implemented in repository.
100        // but the available elementType for Activity are all defined in CMS so anyway it wouldn't work without CMS
101        return getValueOfType(ActivityFactory.AUTHOR, "user");
102    }
103    
104    /**
105     * Generate SAX event to represent all the activity
106     * @param contentHandler the targeted contentHandler
107     * @throws SAXException if an error occurred
108     */
109    public void toSAX(ContentHandler contentHandler) throws SAXException
110    {
111        AttributesImpl attrs = new AttributesImpl();
112        attrs.addCDATAAttribute(ACTIVITY_ID_KEY, getId());
113        attrs.addCDATAAttribute(ACTIVITY_NAME_KEY, getName());
114        XMLUtils.startElement(contentHandler, "activity", attrs);
115        dataToSAX(contentHandler);
116        XMLUtils.endElement(contentHandler, "activity");
117    }
118
119    
120    /**
121     * Serialize the activity as a JSON
122     * @return the JSON
123     */
124    public Map<String, Object> toJSONForClient()
125    {
126        Map<String, Object> json = new HashMap<>();
127        json.put(ACTIVITY_ID_KEY, getId());
128        json.put(ACTIVITY_NAME_KEY, getName());
129        DataContext dataContext = DataContext.newInstance().withObjectId(getId());
130        
131        json.putAll(dataToJSON(dataContext));
132        
133        json.putAll(getActivityType().additionnalDataToJSONForClient(this));
134        
135        return json;
136    }
137}