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.cart;
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.contenttype.ContentTypesHelper;
025import org.ametys.core.group.GroupManager;
026import org.ametys.plugins.repository.AmetysObjectFactory;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.repository.RepositoryConstants;
030import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
031import org.ametys.plugins.repository.jcr.DefaultAmetysObjectFactory;
032import org.ametys.plugins.repository.model.RepeaterDefinition;
033import org.ametys.runtime.model.DefaultElementDefinition;
034import org.ametys.runtime.model.Model;
035import org.ametys.runtime.model.exception.BadItemTypeException;
036import org.ametys.runtime.model.exception.UnknownTypeException;
037import org.ametys.runtime.model.type.ModelItemTypeConstants;
038
039/**
040 * {@link AmetysObjectFactory} for handling {@link Cart}s.
041 */
042public class CartFactory extends DefaultAmetysObjectFactory
043{
044    /** JCR nodetype for cart */
045    public static final String CART_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":cart";
046    
047    /** Group manager */
048    protected GroupManager _groupManager;
049    
050    /** Content type extension point */
051    protected ContentTypeExtensionPoint _cTypeEP;
052    /** Helper for content types */
053    protected ContentTypesHelper _contentTypesHelper;
054    /** Extension point for content types */
055    protected ContentTypeExtensionPoint _contentTypeEP;
056
057    private Model _model;
058    
059    @Override
060    public void service(ServiceManager manager) throws ServiceException
061    {
062        super.service(manager);
063        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
064        _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
065        _contentTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
066        _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
067    }
068    
069    @Override
070    public Cart getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
071    {
072        return new Cart(node, parentPath, this);
073    }
074    
075    AmetysObjectResolver getResolver()
076    {
077        return _resolver;
078    }
079    
080    /**
081     * Group manager getter.
082     * @return The group manager
083     */
084    GroupManager _getGroupsManager()
085    {
086        return _groupManager;
087    }
088    
089    /**
090     * Getter for the content types helper
091     * @return The helper for content types
092     */
093    ContentTypesHelper _getContentTypesHelper()
094    {
095        return _contentTypesHelper;
096    }
097    
098    /**
099     * Getter for the content types helper
100     * @return The helper for content types
101     */
102    ContentTypeExtensionPoint _getContentTypeEP()
103    {
104        return _contentTypeEP;
105    }
106
107    /**
108     * Retrieves the query model
109     * @return the query model
110     */
111    public Model getModel()
112    {
113        if (_model == null)
114        {
115            _model = _createQueryModel();
116        }
117        return _model;
118    }
119    
120    /**
121     * Creates the query model
122     * @return the created model
123     * @throws AmetysRepositoryException if an error occurs.
124     */
125    protected Model _createQueryModel() throws AmetysRepositoryException
126    {
127        try
128        {
129            String role = ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC;
130
131            return Model.of(Cart.class.getName(), Cart.class.getName(),
132                    DefaultElementDefinition.of(Cart.TITLE, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
133                    DefaultElementDefinition.of(Cart.DESCRIPTION, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
134                    DefaultElementDefinition.of(Cart.DOCUMENTATION, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
135                    DefaultElementDefinition.of(Cart.CREATIONDATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role),
136                    DefaultElementDefinition.of(Cart.LASTMODIFICATIONDATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role),
137                    DefaultElementDefinition.of(Cart.AUTHOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
138                    DefaultElementDefinition.of(Cart.CONTRIBUTOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
139                    DefaultElementDefinition.of(Cart.CONTENT_CART_ELEMENTS, true, ModelItemTypeConstants.STRING_TYPE_ID, role),
140                    DefaultElementDefinition.of(Cart.RESOURCE_CART_ELEMENTS, true, ModelItemTypeConstants.STRING_TYPE_ID, role),
141                    DefaultElementDefinition.of(Cart.QUERIES_FROM_DIRECTORY_CART_ELEMENTS, true, ModelItemTypeConstants.STRING_TYPE_ID, role),
142                    RepeaterDefinition.of(Cart.QUERIES_CART_ELEMENTS, role,
143                        DefaultElementDefinition.of(Cart.QUERY_ID_PROPERTY, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
144                        DefaultElementDefinition.of(Cart.QUERY_DESCRIPTION_PROPERTY, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
145                        DefaultElementDefinition.of(Cart.QUERY_AUTHOR_PROPERTY, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
146                        DefaultElementDefinition.of(Cart.QUERY_TITLE_PROPERTY, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
147                        DefaultElementDefinition.of(Cart.QUERY_DATE_PROPERTY, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role)));
148        }
149        catch (UnknownTypeException | BadItemTypeException | ServiceException e)
150        {
151            throw new AmetysRepositoryException("An error occurred while create the query model", e);
152        }
153    }
154}