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