001/*
002 *  Copyright 2010 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.externaldata.data;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.regex.Matcher;
023
024import org.apache.commons.lang.StringUtils;
025
026/**
027 * An abstract query.<br>
028 * Defines getters and setters for all base members of a query, and provides
029 * some helpful methods.
030 */
031public abstract class AbstractQuery implements Query
032{
033    
034    /** The query id. */
035    protected String _id;
036    
037    /** The query name. */
038    protected String _name;
039    
040    /** The query description. */
041    protected String _description;
042    
043    /** The query parameter names. */
044    protected Map<String, String> _parameters;
045    
046    /** The query result type. */
047    protected ResultType _resultType;
048    
049    /** The datasource id. */
050    protected String _dataSourceId;
051    
052    /** The id of source factory */
053    protected String _factoryId;
054    
055    @Override
056    public String getId()
057    {
058        return _id;
059    }
060    
061    /**
062     * Set the query id.
063     * @param id the id of the query
064     */
065    public void setId(String id)
066    {
067        this._id = id;
068    }
069    
070    @Override
071    public String getName()
072    {
073        return _name;
074    }
075    
076    /**
077     * Set the query name.
078     * @param name the name of the query
079     */
080    public void setName(String name)
081    {
082        this._name = name;
083    }
084    
085    @Override
086    public String getDescription()
087    {
088        return _description;
089    }
090    
091    /**
092     * Set the query description.
093     * @param description the description of the query
094     */
095    public void setDescription(String description)
096    {
097        this._description = description;
098    }
099    
100    @Override
101    public ResultType getResultType()
102    {
103        return _resultType;
104    }
105    
106    /**
107     * Set the query result type.
108     * @param resultType the query result type.
109     */
110    public void setResultType(ResultType resultType)
111    {
112        this._resultType = resultType;
113    }
114    
115    @Override
116    public String getFactory()
117    {
118        return _factoryId;
119    }
120    
121    /**
122     * Set the data source factory
123     * @param factoryId The id of source factory
124     */
125    public void setFactory (String factoryId)
126    {
127        _factoryId = factoryId;
128    }
129    
130    @Override
131    public String getDataSourceId()
132    {
133        return _dataSourceId;
134    }
135    
136    /**
137     * Set the id of data source
138     * @param dataSourceId The id of data source
139     */
140    public void setDataSourceId (String dataSourceId)
141    {
142        _dataSourceId = dataSourceId;
143    }
144    
145    /**
146     * Extract parameters from a query string in the form ${parameterName}.
147     * @param query the query containing parameters.
148     * @return the parameter names as a Set.
149     */
150    protected Map<String, String> _buildParameters(String query)
151    {
152        Map<String, String> params = new HashMap<>();
153        
154        if (query != null)
155        {
156            Matcher matcher = Query.PARAMETER_MATCHER.matcher(query);
157            List<String> paramWithLabel = new ArrayList<>();
158            while (matcher.find())
159            {
160                if (matcher.groupCount() == 1)
161                {
162                    // Convert to lowercase to avoid browser problems.
163                    String paramName = matcher.group(1).toLowerCase();
164                    if (!params.containsKey(paramName) || !paramWithLabel.contains(paramName))
165                    {
166                        params.put(paramName, paramName);
167                    }
168                }
169                else if (matcher.groupCount() > 2)
170                {
171                    // Convert to lowercase to avoid browser problems.
172                    String paramName = matcher.group(1).toLowerCase();
173                    String label = matcher.group(3);
174                    if (StringUtils.isBlank(label))
175                    {
176                        label = paramName;
177                        paramWithLabel.add(paramName);
178                    }
179                    
180                    if (!params.containsKey(paramName) || !paramWithLabel.contains(paramName))
181                    {
182                        params.put(paramName, label);
183                    }
184                }
185            }
186        }
187        
188        return params;
189    }
190    
191}