001/*
002 *  Copyright 2016 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.explorer.tasks.jcr;
017
018import java.util.ArrayList;
019import java.util.Date;
020import java.util.List;
021
022import javax.jcr.Node;
023import javax.jcr.RepositoryException;
024
025import org.ametys.core.user.UserIdentity;
026import org.ametys.plugins.explorer.tasks.ModifiableTask;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.RepositoryConstants;
029import org.ametys.plugins.repository.jcr.DefaultAmetysObject;
030import org.ametys.plugins.repository.metadata.ModifiableRichText;
031
032/**
033 * JCR implementation of a task
034 */
035public class JCRTask extends DefaultAmetysObject<JCRTaskFactory> implements ModifiableTask
036{
037    /** Metadata Taskid */
038    public static final String METADATA_TASKID = "taskId";
039    /** Metadata Label */
040    public static final String METADATA_LABEL = "label";
041    /** Metadata Description */
042    public static final String METADATA_DESCRIPTION = "description";
043    /** Metadata Startdate */
044    public static final String METADATA_STARTDATE = "startDate";
045    /** Metadata Enddate */
046    public static final String METADATA_ENDDATE = "endDate";
047    /** Metadata Status */
048    public static final String METADATA_STATUS = "status";
049    /** Metadata Priority */
050    public static final String METADATA_PRIORITY = "priority";
051    /** Metadata Initialload */
052    public static final String METADATA_INITIALLOAD = "initialLoad";
053    /** Metadata Assignment */
054    public static final String METADATA_ASSIGNMENT = "assignment";
055    /** Metadata Subscribers */
056    public static final String METADATA_SUBSCRIBERS = "subscribers";
057    /** Metadata Progress */
058    public static final String METADATA_PROGRESS = "progress";
059    /** Metadata Creationdate */
060    public static final String METADATA_CREATIONDATE = "creationDate";
061    /** Metadata Lastmodified */
062    public static final String METADATA_LASTMODIFIED = "lastModified";
063    /** Metadata Author */
064    public static final String METADATA_AUTHOR = "author";
065    
066    /**
067     * Default constructor for the JCRTask
068     * @param node The task node
069     * @param parentPath The parent path
070     * @param factory The factory
071     */
072    public JCRTask(Node node, String parentPath, JCRTaskFactory factory)
073    {
074        super(node, parentPath, factory);
075    }
076
077    @Override
078    public String getTaskId()
079    {
080        return getMetadataHolder().getString(METADATA_TASKID);
081    }
082
083    @Override
084    public String getLabel()
085    {
086        return getMetadataHolder().getString(METADATA_LABEL, null);
087    }
088
089    @Override
090    public ModifiableRichText getDescription()
091    {
092        return getMetadataHolder().getRichText(METADATA_DESCRIPTION, true);
093    }
094
095    @Override
096    public Date getStartDate()
097    {
098        return getMetadataHolder().getDate(METADATA_STARTDATE, null);
099    }
100
101    @Override
102    public Date getEndDate()
103    {
104        return getMetadataHolder().getDate(METADATA_ENDDATE, null);
105    }
106
107    @Override
108    public TaskStatus getStatus()
109    {
110        return TaskStatus.createsFromString(getMetadataHolder().getString(METADATA_STATUS, TaskStatus.OPEN.toString()));
111    }
112
113    @Override
114    public TaskPriority getPriority()
115    {
116        return TaskPriority.createsFromString(getMetadataHolder().getString(METADATA_PRIORITY, TaskPriority.NORMAL.toString()));
117    }
118
119    @Override
120    public Double getInitialLoad()
121    {
122        return getMetadataHolder().getDouble(METADATA_INITIALLOAD, 0);
123    }
124
125    @Override
126    public Double getProgress()
127    {
128        return getMetadataHolder().getDouble(METADATA_PROGRESS, 0);
129    }
130
131    @Override
132    public List<UserIdentity> getAssignment()
133    {
134        List<UserIdentity> assignment = new ArrayList<>();
135        for (String user : getMetadataHolder().getStringArray(METADATA_ASSIGNMENT, new String[0]))
136        {
137            assignment.add(UserIdentity.stringToUserIdentity(user));
138        }
139        
140        return assignment;
141    }
142
143    @Override
144    public List<UserIdentity> getSubscribers()
145    {
146        List<UserIdentity> subscribers = new ArrayList<>();
147        for (String user : getMetadataHolder().getStringArray(METADATA_SUBSCRIBERS, new String[0]))
148        {
149            subscribers.add(UserIdentity.stringToUserIdentity(user));
150        }
151        
152        return subscribers;
153    }
154    
155    @Override
156    public UserIdentity getAuthor ()
157    {
158        try
159        {
160            Node authorNode = getNode().getNode(RepositoryConstants.NAMESPACE_PREFIX + ":" + METADATA_AUTHOR);
161            return new UserIdentity(authorNode.getProperty("ametys:login").getString(), authorNode.getProperty("ametys:population").getString());
162        }
163        catch (RepositoryException e)
164        {
165            throw new AmetysRepositoryException("Error while getting author property.", e);
166        }
167    }
168
169    @Override
170    public void setTaskId(String taskId)
171    {
172        getMetadataHolder().setMetadata(METADATA_TASKID, taskId);
173    }
174
175    @Override
176    public void setLabel(String title)
177    {
178        if (title != null)
179        {
180            getMetadataHolder().setMetadata(METADATA_LABEL, title);
181        }
182        else if (getMetadataHolder().hasMetadata(METADATA_LABEL))
183        {
184            getMetadataHolder().removeMetadata(METADATA_LABEL);
185        }
186    }
187
188    @Override
189    public void setStartDate(Date startDate)
190    {
191        if (startDate != null)
192        {
193            getMetadataHolder().setMetadata(METADATA_STARTDATE, startDate);
194        }
195        else if (getMetadataHolder().hasMetadata(METADATA_STARTDATE))
196        {
197            getMetadataHolder().removeMetadata(METADATA_STARTDATE);
198        }
199    }
200
201    @Override
202    public void setEndDate(Date endDate)
203    {
204        if (endDate != null)
205        {
206            getMetadataHolder().setMetadata(METADATA_ENDDATE, endDate);
207        }
208        else if (getMetadataHolder().hasMetadata(METADATA_ENDDATE))
209        {
210            getMetadataHolder().removeMetadata(METADATA_ENDDATE);
211        }
212    }
213
214    @Override
215    public void setStatus(TaskStatus status)
216    {
217        if (status.toString() != null)
218        {
219            getMetadataHolder().setMetadata(METADATA_STATUS, status.toString());
220        }
221        else if (getMetadataHolder().hasMetadata(METADATA_STATUS))
222        {
223            getMetadataHolder().removeMetadata(METADATA_STATUS);
224        }
225    }
226    
227    @Override
228    public void setPriority(TaskPriority priority)
229    {
230        if (priority.toString() != null)
231        {
232            getMetadataHolder().setMetadata(METADATA_PRIORITY, priority.toString());
233        }
234        else if (getMetadataHolder().hasMetadata(METADATA_PRIORITY))
235        {
236            getMetadataHolder().removeMetadata(METADATA_PRIORITY);
237        }
238    }
239
240    @Override
241    public void setInitialLoad(Double initialLoad)
242    {
243        if (initialLoad != null)
244        {
245            getMetadataHolder().setMetadata(METADATA_INITIALLOAD, initialLoad);
246        }
247        else if (getMetadataHolder().hasMetadata(METADATA_INITIALLOAD))
248        {
249            getMetadataHolder().removeMetadata(METADATA_INITIALLOAD);
250        }
251    }
252
253    @Override
254    public void setProgress(Double progress)
255    {
256        if (progress != null)
257        {
258            getMetadataHolder().setMetadata(METADATA_PROGRESS, progress);
259        }
260        else if (getMetadataHolder().hasMetadata(METADATA_PROGRESS))
261        {
262            getMetadataHolder().removeMetadata(METADATA_PROGRESS);
263        }
264    }
265
266    @Override
267    public void setAssignment(List<UserIdentity> assignment)
268    {
269        String[] users = new String[assignment.size()];
270        for (int i = 0; i < assignment.size(); i++)
271        {
272            users[i] = UserIdentity.userIdentityToString(assignment.get(i));
273        }
274        
275        getMetadataHolder().setMetadata(METADATA_ASSIGNMENT, users);
276    }
277
278    @Override
279    public void setSubscribers(List<UserIdentity> subscribers)
280    {
281        String[] users = new String[subscribers.size()];
282        for (int i = 0; i < subscribers.size(); i++)
283        {
284            users[i] = UserIdentity.userIdentityToString(subscribers.get(i));
285        }
286        
287        getMetadataHolder().setMetadata(METADATA_SUBSCRIBERS, users);
288    }
289    
290    @Override
291    public Date getCreationDate()
292    {
293        return getMetadataHolder().getDate(METADATA_CREATIONDATE);
294    }
295    
296    @Override
297    public void setCreationDate(Date startDate)
298    {
299        getMetadataHolder().setMetadata(METADATA_CREATIONDATE, startDate);
300    }
301    
302    @Override
303    public Date getLastModified()
304    {
305        return getMetadataHolder().getDate(METADATA_LASTMODIFIED);
306    }
307    
308    @Override
309    public void setLastModified(Date date)
310    {
311        getMetadataHolder().setMetadata(METADATA_LASTMODIFIED, date);
312    }
313    
314    @Override
315    public void setAuthor(UserIdentity author)
316    {
317        try
318        {
319            Node authorNode = null;
320            if (getNode().hasNode(RepositoryConstants.NAMESPACE_PREFIX + ":" + METADATA_AUTHOR))
321            {
322                authorNode = getNode().getNode(RepositoryConstants.NAMESPACE_PREFIX + ":" + METADATA_AUTHOR);
323            }
324            else
325            {
326                authorNode = getNode().addNode(RepositoryConstants.NAMESPACE_PREFIX + ":" + METADATA_AUTHOR, RepositoryConstants.USER_NODETYPE);
327            }
328            
329            authorNode.setProperty("ametys:login", author.getLogin());
330            authorNode.setProperty("ametys:population", author.getPopulationId());
331        }
332        catch (RepositoryException e)
333        {
334            throw new AmetysRepositoryException("Error setting the author property.", e);
335        }
336    }
337    
338}