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