001/*
002 *  Copyright 2015 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.contentstree.ui;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.contenttype.ContentAttributeDefinition;
025import org.ametys.cms.contenttype.ContentType;
026import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.ui.Callable;
029import org.ametys.core.ui.StaticClientSideElement;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.runtime.model.ModelItem;
032
033/**
034 * Static client side element for adding content under the currently selected content
035 */
036public class AddContentToCurrentSelectionClientSideElement extends StaticClientSideElement
037{
038    private AmetysObjectResolver _ametysResolver;
039    private ContentTypeExtensionPoint _contentTypeEP;
040 
041    @Override
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        super.service(smanager);
045        
046        _ametysResolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
047        _contentTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
048    }
049    
050    /**
051     * Get the type of the required model item between content or sub-content
052     * @param contentId The content to check
053     * @param modelItemPath The path to check
054     * @return The type in key "type".
055     */
056    @Callable
057    public Map<String, Object> getModelItemType(String contentId, String modelItemPath)
058    {
059        Map<String, Object> result = new HashMap<>();
060        
061        Content content = _ametysResolver.resolveById(contentId);
062        for (String contentTypeId : content.getTypes())
063        {
064            ContentType contentType = _contentTypeEP.getExtension(contentTypeId);
065            if (contentType.hasModelItem(modelItemPath))
066            {
067                ModelItem modelItem = contentType.getModelItem(modelItemPath);
068                if (modelItem instanceof ContentAttributeDefinition)
069                {
070                    ContentAttributeDefinition contentAttributeDefinition = (ContentAttributeDefinition) modelItem;
071                    result.put("model-item-type", contentAttributeDefinition.getType().getId());
072                    result.put("contenttype", contentAttributeDefinition.getContentTypeId());
073                    result.put("language", content.getLanguage());
074                    break;
075                }
076                else
077                {
078                    throw new IllegalArgumentException("The content '" + contentId + "' has a metadata '" + modelItemPath + "' in content type '" + contentTypeId + "' but its type is neither a CONTENT neither a SUB_CONTENT.");
079                }
080            }
081        }
082        
083        return result;
084    }
085}