|
| 1 | +/******************************************************************************* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2011 - 2015 OpenWorm. |
| 5 | + * http://openworm.org |
| 6 | + * |
| 7 | + * All rights reserved. This program and the accompanying materials |
| 8 | + * are made available under the terms of the MIT License |
| 9 | + * which accompanies this distribution, and is available at |
| 10 | + * http://opensource.org/licenses/MIT |
| 11 | + * |
| 12 | + * Contributors: |
| 13 | + * OpenWorm - http://openworm.org/people.html |
| 14 | + * |
| 15 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 16 | + * of this software and associated documentation files (the "Software"), to deal |
| 17 | + * in the Software without restriction, including without limitation the rights |
| 18 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 19 | + * copies of the Software, and to permit persons to whom the Software is |
| 20 | + * furnished to do so, subject to the following conditions: |
| 21 | + * |
| 22 | + * The above copyright notice and this permission notice shall be included in |
| 23 | + * all copies or substantial portions of the Software. |
| 24 | + * |
| 25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 28 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 29 | + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 30 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 31 | + * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 32 | + *******************************************************************************/ |
| 33 | +package org.geppetto.datasources; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.LinkedHashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import org.eclipse.emf.common.util.EList; |
| 41 | +import org.geppetto.core.datasources.GeppettoDataSourceException; |
| 42 | +import org.geppetto.core.datasources.IDataSourceService; |
| 43 | +import org.geppetto.core.datasources.QueryChecker; |
| 44 | +import org.geppetto.core.model.GeppettoModelAccess; |
| 45 | +import org.geppetto.core.services.AService; |
| 46 | +import org.geppetto.model.datasources.DataSource; |
| 47 | +import org.geppetto.model.datasources.Query; |
| 48 | +import org.geppetto.model.datasources.QueryResults; |
| 49 | +import org.geppetto.model.datasources.RunnableQuery; |
| 50 | +import org.geppetto.model.util.GeppettoModelTraversal; |
| 51 | +import org.geppetto.model.util.GeppettoVisitingException; |
| 52 | +import org.geppetto.model.variables.Variable; |
| 53 | +import org.geppetto.model.variables.VariablesFactory; |
| 54 | + |
| 55 | +/** |
| 56 | + * @author matteocantarelli |
| 57 | + * |
| 58 | + */ |
| 59 | +public abstract class ADataSourceService extends AService implements IDataSourceService |
| 60 | +{ |
| 61 | + |
| 62 | + public abstract ConnectionType getConnectionType(); |
| 63 | + |
| 64 | + public abstract IQueryResponseProcessor getQueryResponseProcessor(); |
| 65 | + |
| 66 | + public enum ConnectionType |
| 67 | + { |
| 68 | + GET, POST |
| 69 | + } |
| 70 | + |
| 71 | + protected IQueryResponseProcessor queryResponseProcessor; |
| 72 | + |
| 73 | + private DataSource configuration; |
| 74 | + |
| 75 | + private String dataSourceTemplate; |
| 76 | + |
| 77 | + private GeppettoModelAccess geppettoModelAccess; |
| 78 | + |
| 79 | + // Cache is shared |
| 80 | + private static Map<String, QueryResults> cachedResults = new LinkedHashMap<String, QueryResults>(); |
| 81 | + private static Map<String, List<String>> cachedIds = new LinkedHashMap<String, List<String>>(); |
| 82 | + |
| 83 | + public ADataSourceService(String dataSourceTemplate) |
| 84 | + { |
| 85 | + this.dataSourceTemplate = dataSourceTemplate; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void initialize(DataSource configuration, GeppettoModelAccess geppettoModelAccess) |
| 90 | + { |
| 91 | + this.configuration = configuration; |
| 92 | + this.geppettoModelAccess = geppettoModelAccess; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int getNumberOfResults(List<RunnableQuery> queries) throws GeppettoDataSourceException |
| 97 | + { |
| 98 | + try |
| 99 | + { |
| 100 | + ExecuteMultipleQueriesVisitor executeMultipleQueriesVisitor = new ExecuteMultipleQueriesVisitor(geppettoModelAccess, cachedResults, cachedIds); |
| 101 | + GeppettoModelTraversal.apply((EList) queries, executeMultipleQueriesVisitor); |
| 102 | + return executeMultipleQueriesVisitor.getCount(); |
| 103 | + } |
| 104 | + catch(GeppettoVisitingException e) |
| 105 | + { |
| 106 | + throw new GeppettoDataSourceException(e); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public QueryResults execute(List<RunnableQuery> queries) throws GeppettoDataSourceException |
| 112 | + { |
| 113 | + try |
| 114 | + { |
| 115 | + ExecuteMultipleQueriesVisitor executeMultipleQueriesVisitor = new ExecuteMultipleQueriesVisitor(geppettoModelAccess, cachedResults, cachedIds); |
| 116 | + GeppettoModelTraversal.apply((EList) queries, executeMultipleQueriesVisitor); |
| 117 | + return executeMultipleQueriesVisitor.getResults(); |
| 118 | + } |
| 119 | + catch(GeppettoVisitingException e) |
| 120 | + { |
| 121 | + throw new GeppettoDataSourceException(e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @return |
| 127 | + */ |
| 128 | + protected GeppettoModelAccess getGeppettoModelAccess() |
| 129 | + { |
| 130 | + return geppettoModelAccess; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @return |
| 135 | + */ |
| 136 | + protected String getTemplate() |
| 137 | + { |
| 138 | + return dataSourceTemplate; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @return the configuration for this DataSourceService |
| 143 | + */ |
| 144 | + protected DataSource getConfiguration() |
| 145 | + { |
| 146 | + return configuration; |
| 147 | + } |
| 148 | + |
| 149 | + /* |
| 150 | + * (non-Javadoc) |
| 151 | + * |
| 152 | + * @see org.geppetto.core.model.QueryProvider#getAvailableQueries(org.geppetto.model.variables.Variable) |
| 153 | + */ |
| 154 | + @Override |
| 155 | + public List<Query> getAvailableQueries(Variable variable) |
| 156 | + { |
| 157 | + List<Query> availableQueries = new ArrayList<Query>(); |
| 158 | + for(Query query : configuration.getQueries()) |
| 159 | + { |
| 160 | + if(QueryChecker.check(query, variable)) |
| 161 | + { |
| 162 | + availableQueries.add(query); |
| 163 | + } |
| 164 | + } |
| 165 | + return availableQueries; |
| 166 | + } |
| 167 | + |
| 168 | + /* |
| 169 | + * (non-Javadoc) |
| 170 | + * |
| 171 | + * @see org.geppetto.core.model.IDataSource#fetchVariable(java.lang.String) |
| 172 | + */ |
| 173 | + @Override |
| 174 | + public void fetchVariable(String variableId) throws GeppettoDataSourceException |
| 175 | + { |
| 176 | + Variable fetchedVariable = VariablesFactory.eINSTANCE.createVariable(); |
| 177 | + fetchedVariable.setId(variableId); |
| 178 | + getGeppettoModelAccess().addVariable(fetchedVariable); |
| 179 | + Query fetchVariableQuery = getConfiguration().getFetchVariableQuery(); |
| 180 | + ExecuteQueryVisitor runQueryVisitor = new ExecuteQueryVisitor(fetchedVariable, getGeppettoModelAccess()); |
| 181 | + runQueryVisitor.doSwitch(fetchVariableQuery); |
| 182 | + } |
| 183 | + |
| 184 | + /* |
| 185 | + * (non-Javadoc) |
| 186 | + * |
| 187 | + * @see org.geppetto.core.services.IService#registerGeppettoService() |
| 188 | + */ |
| 189 | + @Override |
| 190 | + public void registerGeppettoService() throws Exception |
| 191 | + { |
| 192 | + // Nothing to do here |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments