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.cms.content;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032
033import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
034import org.ametys.cms.lock.LockContentManager;
035import org.ametys.cms.repository.Content;
036import org.ametys.cms.repository.ContentDAO;
037import org.ametys.cms.workflow.ContentWorkflowHelper;
038import org.ametys.core.cocoon.JSonReader;
039import org.ametys.core.right.RightManager;
040import org.ametys.core.user.CurrentUserProvider;
041import org.ametys.plugins.repository.AmetysObjectResolver;
042import org.ametys.plugins.repository.UnknownAmetysObjectException;
043
044/**
045 * Get the content information
046 *
047 */
048public class GetContentInformationAction extends ServiceableAction
049{
050    /** The Ametys object resolver */
051    protected AmetysObjectResolver _resolver;
052    /** The current user provider */
053    protected CurrentUserProvider _userProvider;
054    /** The rights manager */
055    protected RightManager _rightManager;
056    /** The content type extension point */
057    protected ContentTypeExtensionPoint _contentTypeEP;
058    /** The lock manager. */
059    protected LockContentManager _lockManager;
060    /** The content workflow helper */
061    protected ContentWorkflowHelper _contentWorkflowHelper;
062    /** DAO for contents */
063    protected ContentDAO _contentDAO;
064    
065    @Override
066    public void service(ServiceManager smanager) throws ServiceException
067    {
068        super.service(smanager);
069        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
070        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
071        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
072        _contentTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
073        _lockManager = (LockContentManager) smanager.lookup(LockContentManager.ROLE);
074        _contentWorkflowHelper = (ContentWorkflowHelper) smanager.lookup(ContentWorkflowHelper.ROLE);
075        _contentDAO = (ContentDAO) smanager.lookup(ContentDAO.ROLE);
076    }
077    
078    @Override
079    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
080    {
081        @SuppressWarnings("unchecked")
082        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
083        
084        @SuppressWarnings("unchecked")
085        List<String> contentIds = (List<String>) jsParameters.get("ids");
086        
087        Map<String, Object> result = new HashMap<>();
088        
089        List<Map<String, Object>> contents = new ArrayList<>();
090        List<String> contentsNotFound = new ArrayList<>();
091        
092        for (String contentId : contentIds)
093        {
094            try
095            {
096                Content content = _resolver.resolveById(contentId);
097                contents.add(_contentDAO.getContentProperties(content));
098            }
099            catch (UnknownAmetysObjectException e)
100            {
101                contentsNotFound.add(contentId);
102            }
103        }
104        
105        result.put("contents", contents);
106        result.put("contentsNotFound", contentsNotFound);
107        
108        Request request = ObjectModelHelper.getRequest(objectModel);
109        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
110        
111        return EMPTY_MAP;
112    }
113    
114    /**
115     * Get content additional properties
116     * @param content The content
117     * @return The additionnal properties
118     */
119    protected Map<String, Object> getAdditionalProperties (Content content)
120    {
121        return Collections.EMPTY_MAP;
122    }
123}