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.ContentType;
025import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
026import org.ametys.cms.contenttype.MetadataDefinition;
027import org.ametys.cms.contenttype.MetadataType;
028import org.ametys.cms.repository.Content;
029import org.ametys.core.ui.Callable;
030import org.ametys.core.ui.StaticClientSideElement;
031import org.ametys.plugins.repository.AmetysObjectResolver;
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 metadata between content or sub-content
052     * @param contentId The content to check
053     * @param metadataPath The path to check
054     * @return The type in key "type".
055     */
056    @Callable
057    public Map<String, Object> getMetadataType(String contentId, String metadataPath)
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            MetadataDefinition metadataDef = contentType.getMetadataDefinitionByPath(metadataPath);
066            if (metadataDef != null)
067            {
068                if (metadataDef.getType() == MetadataType.CONTENT || metadataDef.getType() == MetadataType.SUB_CONTENT)
069                {
070                    result.put("metadata-type", metadataDef.getType().toString().toLowerCase());
071                    result.put("contenttype", metadataDef.getContentType());
072                    result.put("language", content.getLanguage());
073                    break;
074                }
075                else
076                {
077                    throw new IllegalArgumentException("The content '" + contentId + "' has a metadata '" + metadataPath + "' in content type '" + contentTypeId + "' but its type is neither a CONTENT neither a SUB_CONTENT.");
078                }
079            }
080        }
081        
082        return result;
083    }
084}