001/*
002 * Copyright 2017 the original author or authors.
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package net.sf.jkniv.camel.sap.jco3;
020
021import java.util.Properties;
022
023import org.apache.camel.Consumer;
024import org.apache.camel.Processor;
025import org.apache.camel.Producer;
026import org.apache.camel.RuntimeCamelException;
027import org.apache.camel.impl.DefaultEndpoint;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.UriEndpoint;
030import org.apache.camel.spi.UriParam;
031import org.apache.camel.spi.UriPath;
032
033/**
034 * Represents a sap jco endpoint.
035 */
036@UriEndpoint(scheme = "jkniv-jco", title = "jkniv-jco", syntax = "jkniv-jco:name", label = "jkniv-jco")
037public class SapJcoEndpoint extends DefaultEndpoint
038{
039    @UriPath
040    @Metadata(required = "true")
041    private String     name;
042    
043    @UriParam(defaultValue = "")
044    private String     sapFunction          = "";
045    
046    @UriParam(defaultValue = "")
047    private String     sapDestName          = "";
048    
049    @UriParam(defaultValue = "")
050    private String     sapJcoTable          = "";
051
052    @UriParam(defaultValue = "")
053    private String     sapJcoTableIn          = "";
054
055    @UriParam(defaultValue = "SAPJco_")
056    private String     prefixParams         = "SAPJco_";
057    
058    @UriParam(defaultValue = "net.sf.jkniv.camel.sap.jco3.MapParserResult")
059    private String     parserResultStrategy = "net.sf.jkniv.camel.sap.jco3.MapParserResult";
060    
061    @UriParam(defaultValue = "false", description="The parameters are in header from Message, default is false")
062    private boolean    useHeaderAsParam     = false;
063
064    @UriParam(defaultValue = "false", description="Result can be contains null values, default is false")
065    private boolean    supportsNull = false;
066
067    private Class     classToParserResultStrategy;
068    private Properties propSapConnection;
069    
070    public SapJcoEndpoint()
071    {
072    }
073    
074    public SapJcoEndpoint(String uri, SapJcoComponent component)
075    {
076        super(uri, component);
077    }
078    
079    public SapJcoEndpoint(String endpointUri)
080    {
081        super(endpointUri);
082    }
083    
084    public Producer createProducer() throws Exception
085    {
086        return new SapJcoProducer(this);
087    }
088    
089    /*
090    public Consumer createConsumer(Processor processor) throws Exception {
091        return new SapJcoConsumer(this, processor);
092    }
093    */
094    public Consumer createConsumer(Processor processor) throws Exception
095    {
096        throw new UnsupportedOperationException("Not supported operation createConsumer");
097    }
098    
099    public boolean isSingleton()
100    {
101        return true;
102    }
103    
104    /**
105     * Some description of this option, and what it does
106     * @param name endpoint
107     */
108    public void setName(String name)
109    {
110        this.name = name;
111    }
112    
113    public String getName()
114    {
115        return name;
116    }
117    
118    /**
119     * The name of SAP <code>JCoFunction</code>
120     * @return the name of function
121     * @see com.sap.conn.jco.JCoFunction
122     */
123    public String getSapFunction()
124    {
125        return sapFunction;
126    }
127    
128    /**
129     * Define the name of SAP <code>JCoFunction</code>
130     * @param sapFunction name of SAP function
131     */
132    public void setSapFunction(String sapFunction)
133    {
134        this.sapFunction = sapFunction;
135    }
136    
137    /**
138     * The name of SAP <code>JCoDestination</code> name that identifies a physical destination of a function call. 
139     * It contains all required properties in order to connect to an SAP system. 
140     * @return the name of <code>JCoDestination</code>
141     * @see com.sap.conn.jco.JCoDestination
142     */
143    public String getSapDestName()
144    {
145        return sapDestName;
146    }
147    
148    /**
149     * Set the name of SAP <code>JCoDestination</code> name that identifies a physical destination of a function call
150     * @param sapDestName the name of <code>JCoDestination</code>
151     */
152    public void setSapDestName(String sapDestName)
153    {
154        this.sapDestName = sapDestName;
155    }
156    
157    /**
158     * The name of SAP <code>JCoTable</code> that encapsulates a database table 
159     * @return the name of SAP <code>JCoTable</code> .
160     * @see com.sap.conn.jco.JCoTable
161     */
162    public String getSapJcoTable()
163    {
164        return sapJcoTable;
165    }
166    
167    /**
168     * The name of SAP <code>JCoTable</code> 
169     * @param sapJcoTable name of SAP <code>JCoTable</code> 
170     */
171    public void setSapJcoTable(String sapJcoTable)
172    {
173        this.sapJcoTable = sapJcoTable;
174    }
175    
176    /**
177     * The name of SAP <code>JCoTable</code> that encapsulates a input of table parameters 
178     * @return the name of SAP <code>JCoTable</code> .
179     * @see com.sap.conn.jco.JCoTable
180     */
181    public String getSapJcoTableIn()
182    {
183        return sapJcoTableIn;
184    }
185    
186    
187    /**
188     * The name of SAP <code>JCoTable</code> 
189     * @param sapJcoTableIn name of SAP <code>JCoTable</code> 
190     */
191    public void setSapJcoTableIn(String sapJcoTableIn)
192    {
193        this.sapJcoTableIn = sapJcoTableIn;
194    }
195    
196    /**
197     * The prefix name of parameters. Default is <code>SAPJco_</code>
198     * @return prefix name of JCo parameters
199     */
200    public String getPrefixParams()
201    {
202        return prefixParams;
203    }
204    
205    /**
206     * Define prefix name for JCo parameters. Default is <code>SAPJco_</code>
207     * 
208     * @param prefixParams name of prefix
209     */
210    public void setPrefixParams(String prefixParams)
211    {
212        this.prefixParams = prefixParams;
213    }
214    
215    /**
216     * Parameters of JCo.
217     * @return true if parameters from JCo come from header message, false otherwise.
218     */
219    public boolean isUseHeaderAsParam()
220    {
221        return useHeaderAsParam;
222    }
223    
224    /**
225     * Define if JCo parameters are in header message.
226     * @param useHeaderAsParam parameters from JCo come from header message
227     */
228    public void setUseHeaderAsParam(boolean useHeaderAsParam)
229    {
230        this.useHeaderAsParam = useHeaderAsParam;
231    }
232    
233    /**
234     * When <code>supportsNull</code> is <code>true</code> the list of result could be null values.
235     * 
236     * @return <code>false</code> if result of JCo function supports <code>null</code> values, <code>true</code> otherwise.
237     */
238    public boolean isSupportsNull()
239    {
240        return supportsNull;
241    }
242    
243    /**
244     * set <code>true</code> to support null values at result list.
245     * 
246     * @param supportsNull indicate if result list could be contains <code>null</code> values. 
247     */
248    public void setSupportsNull(boolean supportsNull)
249    {
250        this.supportsNull = supportsNull;
251    }
252    
253    /**
254     * Class name from implementation of {@code ParserResult}
255     * @return the name of concrete class that implements {@code ParserResult}
256     */
257    public String getParserResultStrategy()
258    {
259        return parserResultStrategy;
260    }
261    
262    public <T> Class<T> getClassToParserResultStrategy()
263    {
264        if (classToParserResultStrategy == null)
265        {
266            classToParserResultStrategy = forName(parserResultStrategy);
267        }
268        return classToParserResultStrategy;
269    }
270    
271    /**
272     * Define the class name that implements {@code ParserResult}
273     * @param parserResultStrategy class name to parse result 
274     * @throws RuntimeCamelException when {@code net.sf.jkniv.camel.sap.jco3.ParserResult} implementation cannot be found
275     */
276    public void setParserResultStrategy(String parserResultStrategy)
277    {
278        classToParserResultStrategy = forName(parserResultStrategy);
279        this.parserResultStrategy = parserResultStrategy;
280    }
281    
282    private Class forName(String parserResultStrategy)
283    {
284        try
285        {
286            Class clazz = SapJcoEndpoint.class.forName(parserResultStrategy);
287            if (ParserResult.class.isAssignableFrom(clazz))
288                return clazz;
289        }
290        catch (ClassNotFoundException e)
291        {
292        }
293        // must be not reach here
294        throw new RuntimeCamelException(
295                "Class [" + parserResultStrategy + "] doesn't assignable to [" + ParserResult.class.getName() + "] or not found at classpath");
296    }
297    
298    public void setPropSapConnection(Properties propSapConnection)
299    {
300        this.propSapConnection = propSapConnection;
301    }
302    
303    public Properties getPropSapConnection()
304    {
305        return propSapConnection;
306    }
307}