001/*
002 *  Copyright 2013 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.queriesdirectory;
017
018import java.time.ZonedDateTime;
019
020import javax.jcr.Node;
021
022import org.ametys.cms.indexing.solr.SolrAclCacheUninfluentialObject;
023import org.ametys.core.user.UserIdentity;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.plugins.repository.MovableAmetysObject;
027import org.ametys.plugins.repository.RepositoryIntegrityViolationException;
028import org.ametys.plugins.repository.data.ametysobject.ModifiableModelAwareDataAwareAmetysObject;
029import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
030import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelAwareDataHolder;
031import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
032import org.ametys.plugins.repository.jcr.DefaultAmetysObject;
033
034/**
035 * Class representing a query, backed by a JCR node.<br>
036 */
037@SolrAclCacheUninfluentialObject
038public class Query extends DefaultAmetysObject<QueryFactory> implements ModifiableModelAwareDataAwareAmetysObject, MovableAmetysObject
039{
040
041    /** Property name for query title */
042    public static final String TITLE = "label";
043    /** Property name for query type */
044    public static final String TYPE = "type";
045    /** Property name for query description */
046    public static final String DESCRIPTION = "description";
047    /** Property name for query documentation */
048    public static final String DOCUMENTATION = "documentation";
049    /** Property name for query content */
050    public static final String CONTENT = "content";
051    /** Property name for query author */
052    public static final String AUTHOR = "author";
053    /** Property name for query last contributor */
054    public static final String CONTRIBUTOR = "contributor";
055    /** Property name for query creation date */
056    public static final String CREATIONDATE = "creationDate";
057    /** Property name for query last modification date */
058    public static final String LASTMODIFICATIONDATE = "lastModificationDate";
059
060    /**
061     * Type of a Query
062     */
063    public enum Type
064    {
065        /** Simple type. */
066        SIMPLE,
067        /** Default type. */
068        ADVANCED,
069        /** Script type. */
070        SCRIPT;
071        
072        @Override
073        public String toString()
074        {
075            return name().toLowerCase();
076        }
077    }
078    
079    /**
080     * Rights profiles
081     */
082    public enum QueryProfile
083    {
084        /** Read access */
085        READ_ACCESS,
086        /** Write access */
087        WRITE_ACCESS,
088        /** Right access */
089        RIGHT_ACCESS;
090        
091        @Override
092        public String toString()
093        {
094            return name().toLowerCase();
095        }
096        
097    }
098
099    /**
100     * Creates an {@link Query}.
101     * @param node the node backing this {@link AmetysObject}
102     * @param parentPath the parentPath in the Ametys hierarchy
103     * @param factory the DefaultAmetysObjectFactory which created the AmetysObject
104     */
105    public Query(Node node, String parentPath, QueryFactory factory)
106    {
107        super(node, parentPath, factory);
108    }
109    
110    /**
111     * Set the title of this query.
112     * @param title the description
113     */
114    public void setTitle (String title)
115    {
116        this.setValue(TITLE, title);
117    }
118    
119    /**
120     * Set the type of this query.
121     * @param type the description
122     */
123    public void setType (String type)
124    {
125        this.setValue(TYPE, type);
126    }
127    
128    /**
129     * Set the description of this query.
130     * @param description the description
131     */
132    public void setDescription (String description)
133    {
134        this.setValue(DESCRIPTION, description);
135    }
136    
137    /**
138     * Set the documentation of this query.
139     * @param documentation the documentation
140     */
141    public void setDocumentation (String documentation)
142    {
143        this.setValue(DOCUMENTATION, documentation);
144    }
145    
146    /**
147     * Set the content of this query.
148     * @param content the content
149     */
150    public void setContent (String content)
151    {
152        this.setValue(CONTENT, content);
153    }
154
155    /**
156     * Set the author of this query.
157     * @param author the author
158     */
159    public void setAuthor(UserIdentity author)
160    {
161        this.setValue(AUTHOR, author);
162    }
163
164    /**
165     * Set the last contributor of this query.
166     * @param contributor the last contributor
167     */
168    public void setContributor(UserIdentity contributor)
169    {
170        this.setValue(CONTRIBUTOR, contributor);
171    }
172    
173    /**
174     * Set the date of the creation.
175     * @param creationDate the last modification date to set.
176     */
177    public void setCreationDate(ZonedDateTime creationDate)
178    {
179        this.setValue(CREATIONDATE, creationDate);
180    }
181    
182    /**
183     * Set the date of the last modification.
184     * @param lastModificationDate the last modification date to set.
185     */
186    public void setLastModificationDate(ZonedDateTime lastModificationDate)
187    {
188        this.setValue(LASTMODIFICATIONDATE, lastModificationDate);
189    }
190    
191    /**
192     * Get the title of the query
193     * @return The title
194     */
195    public String getTitle()
196    {        
197        return this.getValue(TITLE);
198    }
199    
200    /**
201     * Get the type of the query
202     * @return The type
203     */
204    public String getType()
205    {        
206        return this.getValue(TYPE);
207    }
208
209    
210    /**
211     * Get the description of the query
212     * @return The description
213     */
214    public String getDescription()
215    {
216        return this.getValue(DESCRIPTION);
217    }
218    
219    /**
220     * Get the documentation of the query
221     * @return The documentation
222     */
223    public String getDocumentation()
224    {
225        return this.getValue(DOCUMENTATION);
226    }
227    
228    /**
229     * Get the content of the query
230     * @return The content
231     */
232    public String getContent()
233    {
234        return this.getValue(CONTENT);
235    }
236    
237
238    /**
239     * Get the author of the query
240     * @return The author
241     */
242    public UserIdentity getAuthor ()
243    {
244        return this.getValue(AUTHOR);
245    }
246    
247    /**
248     * Get the last contributor of the query
249     * @return The contributor
250     */
251    public UserIdentity getContributor()
252    {
253        return this.getValue(CONTRIBUTOR);
254    }
255    
256    /**
257     * Get the date of the last modification of the query
258     * @return The date
259     */
260    public ZonedDateTime getCreationDate()
261    {
262        return this.getValue(CREATIONDATE);
263    }
264    
265    /**
266     * Get the date of the last modification of the query
267     * @return The date
268     */
269    public ZonedDateTime getLastModificationDate()
270    {
271        return this.getValue(LASTMODIFICATIONDATE);
272    }
273    
274    @Override
275    public boolean canMoveTo(AmetysObject newParent) throws AmetysRepositoryException
276    {
277        return QueryAndContainerCommonMethods.canMoveTo(newParent, this, _getFactory().getQueryDAO());
278    }
279    
280    @Override
281    public void moveTo(AmetysObject newParent, boolean renameIfExist) throws AmetysRepositoryException, RepositoryIntegrityViolationException
282    {
283        QueryAndContainerCommonMethods.moveTo(newParent, renameIfExist, this);
284    }
285    
286    @Override
287    public void orderBefore(AmetysObject siblingNode) throws AmetysRepositoryException
288    {
289        QueryAndContainerCommonMethods.orderBefore(siblingNode, this);
290    }
291
292    public ModifiableModelAwareDataHolder getDataHolder()
293    {
294        JCRRepositoryData repositoryData = new JCRRepositoryData(getNode());
295        return new DefaultModifiableModelAwareDataHolder(repositoryData, this._getFactory().getModel());
296    }
297
298}