001/*
002 *  Copyright 2017 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.workspaces.cmis;
017
018import java.math.BigInteger;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.chemistry.opencmis.commons.definitions.MutableDocumentTypeDefinition;
023import org.apache.chemistry.opencmis.commons.definitions.MutableFolderTypeDefinition;
024import org.apache.chemistry.opencmis.commons.definitions.MutablePropertyDefinition;
025import org.apache.chemistry.opencmis.commons.definitions.MutableTypeDefinition;
026import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
027import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
028import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
029import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
030import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
031import org.apache.chemistry.opencmis.commons.server.CallContext;
032import org.apache.chemistry.opencmis.server.support.TypeDefinitionFactory;
033
034/**
035 * Manages the type definitions for all FileShare repositories.
036 */
037public class CmisTypeManager
038{
039    private static final String NAMESPACE = "http://ametysorg/opencmis";
040
041    private final TypeDefinitionFactory _typeDefinitionFactory;
042    private final Map<String, TypeDefinition> _typeDefinitions;
043
044    /**
045     * Type Manager
046     */
047    public CmisTypeManager()
048    {
049        // set up TypeDefinitionFactory
050        _typeDefinitionFactory = TypeDefinitionFactory.newInstance();
051        _typeDefinitionFactory.setDefaultNamespace(NAMESPACE);
052        _typeDefinitionFactory.setDefaultControllableAcl(false);
053        _typeDefinitionFactory.setDefaultControllablePolicy(false);
054        _typeDefinitionFactory.setDefaultQueryable(false);
055        _typeDefinitionFactory.setDefaultFulltextIndexed(false);
056        _typeDefinitionFactory.setDefaultTypeMutability(_typeDefinitionFactory.createTypeMutability(false, false, false));
057
058        // set up definitions map
059        _typeDefinitions = new HashMap<>();
060
061        // add base folder type
062        MutableFolderTypeDefinition folderType = _typeDefinitionFactory
063                .createBaseFolderTypeDefinition(CmisVersion.CMIS_1_1);
064        removeQueryableAndOrderableFlags(folderType);
065        _typeDefinitions.put(folderType.getId(), folderType);
066
067        // add base document type
068        MutableDocumentTypeDefinition documentType = _typeDefinitionFactory
069                .createBaseDocumentTypeDefinition(CmisVersion.CMIS_1_1);
070        removeQueryableAndOrderableFlags(documentType);
071        _typeDefinitions.put(documentType.getId(), documentType);
072    }
073
074    /*
075     * Removes the queryable and orderable flags from the property definitions
076     * of a type definition because this implementations does neither support
077     * queries nor can order objects.
078     */
079    private void removeQueryableAndOrderableFlags(MutableTypeDefinition type)
080    {
081        for (PropertyDefinition<?> propDef : type.getPropertyDefinitions().values())
082        {
083            MutablePropertyDefinition<?> mutablePropDef = (MutablePropertyDefinition<?>) propDef;
084            mutablePropDef.setIsQueryable(false);
085            mutablePropDef.setIsOrderable(false);
086        }
087    }
088
089    /**
090     * Returns the internal type definition.
091     * @param typeId String
092     * @return Type definition
093     */
094    public synchronized TypeDefinition getInternalTypeDefinition(String typeId)
095    {
096        return _typeDefinitions.get(typeId);
097    }
098
099    // --- service methods ---
100
101    /**
102     * Get the type definition
103     * @param context call context
104     * @param typeId type id
105     * @return type definition
106     */
107    public TypeDefinition getTypeDefinition(CallContext context, String typeId)
108    {
109        TypeDefinition type = _typeDefinitions.get(typeId);
110        if (type == null)
111        {
112            throw new CmisObjectNotFoundException("Type '" + typeId + "' is unknown!");
113        }
114
115        return _typeDefinitionFactory.copy(type, true, context.getCmisVersion());
116    }
117
118    /**
119     * get the type children
120     * @param context call context
121     * @param typeId type
122     * @param includePropertyDefinitions excludes
123     * @param maxItems max
124     * @param skipCount skip
125     * @return list of type definition
126     */
127    public TypeDefinitionList getTypeChildren(CallContext context, String typeId, Boolean includePropertyDefinitions,
128            BigInteger maxItems, BigInteger skipCount)
129    {
130        return _typeDefinitionFactory.createTypeDefinitionList(_typeDefinitions, typeId, includePropertyDefinitions,
131                maxItems, skipCount, context.getCmisVersion());
132    }
133
134    @Override
135    public String toString()
136    {
137        StringBuilder sb = new StringBuilder();
138
139        for (TypeDefinition type : _typeDefinitions.values())
140        {
141            sb.append('[');
142            sb.append(type.getId());
143            sb.append(" (");
144            sb.append(type.getBaseTypeId().value());
145            sb.append(")]");
146        }
147
148        return sb.toString();
149    }
150}