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 javax.jcr.Node;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
024import org.ametys.cms.model.ModelAwareBasicTypesExtensionPoint;
025import org.ametys.core.group.GroupManager;
026import org.ametys.plugins.repository.AmetysObjectFactory;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.RepositoryConstants;
029import org.ametys.plugins.repository.jcr.DefaultAmetysObjectFactory;
030import org.ametys.runtime.model.ElementDefinition;
031import org.ametys.runtime.model.Model;
032import org.ametys.runtime.model.exception.BadItemTypeException;
033import org.ametys.runtime.model.exception.UnknownTypeException;
034import org.ametys.runtime.model.type.ModelItemTypeConstants;
035
036
037/**
038 * {@link AmetysObjectFactory} for handling {@link Query}s.
039 */
040public class QueryFactory extends DefaultAmetysObjectFactory
041{
042    
043    /** JCR nodetype for query */
044    public static final String QUERY_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":query";
045    
046    /** Group manager */
047    protected GroupManager _groupManager;
048    
049    /** Content type extension point */
050    protected ContentTypeExtensionPoint _cTypeEP;
051
052    /** The Query DAO */
053    protected QueryDAO _queryDAO;
054    
055    /** The Query model */
056    protected Model _model;
057
058    @Override
059    public void service(ServiceManager manager) throws ServiceException
060    {
061        super.service(manager);
062        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
063        _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
064        _queryDAO = (QueryDAO) manager.lookup(QueryDAO.ROLE);
065    }
066    
067    @Override
068    public Query getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
069    {
070        return new Query(node, parentPath, this);
071    }
072    
073    /**
074     * Group manager getter.
075     * @return The group manager
076     */
077    GroupManager _getGroupManager()
078    {
079        return _groupManager;
080    }
081    
082    /**
083     * Getter for the content type extension point
084     * @return The content type EP.
085     */
086    ContentTypeExtensionPoint _getContentTypeExtensionPoint()
087    {
088        return _cTypeEP;
089    }
090    
091    QueryDAO getQueryDAO()
092    {
093        return _queryDAO;
094    }
095
096    /**
097     * Retrieves the query model
098     * @return the query model
099     */
100    public Model getModel()
101    {
102        if (_model == null)
103        {
104            _model = _createQueryModel();
105        }
106        return _model;
107    }
108    
109    /**
110     * Creates the query model
111     * @return the created model
112     * @throws AmetysRepositoryException if an error occurs.
113     */
114    protected Model _createQueryModel() throws AmetysRepositoryException
115    {
116        try
117        {
118            String role = ModelAwareBasicTypesExtensionPoint.ROLE;
119            return Model.of(Query.class.getName(), Query.class.getName(),
120                    ElementDefinition.of(Query.TITLE, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
121                    ElementDefinition.of(Query.DESCRIPTION, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
122                    ElementDefinition.of(Query.DOCUMENTATION, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
123                    ElementDefinition.of(Query.CONTENT, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
124                    ElementDefinition.of(Query.TYPE, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
125                    ElementDefinition.of(Query.CREATIONDATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role),
126                    ElementDefinition.of(Query.LASTMODIFICATIONDATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role),
127                    ElementDefinition.of(Query.AUTHOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
128                    ElementDefinition.of(Query.CONTRIBUTOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role));
129        }
130        catch (UnknownTypeException | BadItemTypeException | ServiceException e)
131        {
132            throw new AmetysRepositoryException("An error occurred while creating the query model", e);
133        }
134    }
135}