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.util.HashSet;
019import java.util.Set;
020
021import org.apache.chemistry.opencmis.commons.PropertyIds;
022import org.apache.chemistry.opencmis.commons.data.Properties;
023import org.apache.chemistry.opencmis.commons.data.PropertyData;
024import org.apache.chemistry.opencmis.commons.data.PropertyId;
025import org.apache.chemistry.opencmis.commons.data.PropertyString;
026import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
027
028/**
029 * utils class for CMIS server
030 *
031 */
032public final class CmisUtils
033{
034
035    private CmisUtils()
036    {
037        //nothing here
038    }
039    /**
040     * Returns the boolean value of the given value or the default value if the
041     * given value is <code>null</code>.
042     * @param value value
043     * @param def default
044     * @return boolean
045     */
046    public static boolean getBooleanParameter(Boolean value, boolean def)
047    {
048        if (value == null)
049        {
050            return def;
051        }
052
053        return value.booleanValue();
054    }
055    /**
056     * Returns the first value of a string property.
057     * @param properties property
058     * @param name name
059     * @return String
060     */
061    public static String getStringProperty(Properties properties, String name)
062    {
063        return getStringProperty(properties, name, null);
064    }
065    /**
066     * get String property
067     * @param properties property
068     * @param name name
069     * @param defaultValue default value
070     * @return String
071     */
072    public static String getStringProperty(Properties properties, String name, String defaultValue)
073    {
074        PropertyData<?> property = properties.getProperties().get(name);
075        if (!(property instanceof PropertyString))
076        {
077            return defaultValue;
078        }
079
080        return ((PropertyString) property).getFirstValue();
081    }
082    
083    /**
084     * Gets the type id from a set of properties.
085     * @param properties properties
086     * @return type id
087     */
088    public static String getObjectTypeId(Properties properties)
089    {
090        PropertyData<?> typeProperty = properties.getProperties().get(PropertyIds.OBJECT_TYPE_ID);
091        if (!(typeProperty instanceof PropertyId))
092        {
093            throw new CmisInvalidArgumentException("Type Id must be set!");
094        }
095
096        String typeId = ((PropertyId) typeProperty).getFirstValue();
097        if (typeId == null)
098        {
099            throw new CmisInvalidArgumentException("Type Id must be set!");
100        }
101
102        return typeId;
103    }
104
105    /**
106     * Splits a filter statement into a collection of properties. If
107     * <code>filter</code> is <code>null</code>, empty or one of the properties
108     * is '*' , an empty collection will be returned.
109     * @param filter input filters
110     * @return set of filters, or null if no filters
111     */
112    public static Set<String> splitFilter(String filter)
113    {
114        if (filter == null || filter.trim().length() == 0)
115        {
116            return null;
117        }
118
119        Set<String> result = new HashSet<>();
120        for (String s : filter.split(","))
121        {
122            String trimmed = s.trim();
123            if (trimmed.equals("*") || trimmed.equals(org.apache.chemistry.opencmis.commons.impl.Constants.RENDITION_NONE))
124            {
125                return null;
126            }
127            else if (trimmed.length() > 0)
128            {
129                result.add(trimmed);
130            }
131        }
132
133        // set a few base properties
134        // query name == id (for base type properties)
135        result.add(PropertyIds.OBJECT_ID);
136        result.add(PropertyIds.OBJECT_TYPE_ID);
137        result.add(PropertyIds.BASE_TYPE_ID);
138
139        return result;
140    }
141}