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