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