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