View Javadoc
1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.cassandra;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.synchronoss.cpo.CpoByteArrayInputStream;
26  import org.synchronoss.cpo.CpoCharArrayReader;
27  import org.synchronoss.cpo.CpoException;
28  import org.synchronoss.cpo.cassandra.meta.CassandraMethodMapEntry;
29  import org.synchronoss.cpo.cassandra.meta.CassandraMethodMapper;
30  import org.synchronoss.cpo.cassandra.transform.CassandraCpoTransform;
31  import org.synchronoss.cpo.helper.ExceptionHelper;
32  import org.synchronoss.cpo.meta.AbstractBindableCpoData;
33  import org.synchronoss.cpo.meta.domain.CpoAttribute;
34  import org.synchronoss.cpo.transform.CpoTransform;
35  
36  import java.io.InputStream;
37  import java.io.Reader;
38  
39  /**
40   *
41   * @author dberry
42   */
43  public class CassandraBoundStatementCpoData extends AbstractBindableCpoData {
44  
45    private static final Logger logger = LoggerFactory.getLogger(CassandraBoundStatementCpoData.class);
46    private CassandraBoundStatementFactory cpoStatementFactory = null;
47  
48    public CassandraBoundStatementCpoData(CassandraBoundStatementFactory cpoStatementFactory, CpoAttribute cpoAttribute, int index) {
49      super(cpoAttribute, index);
50      this.cpoStatementFactory = cpoStatementFactory;
51    }
52  
53    @Override
54    public void invokeSetter(Object instanceObject) throws CpoException {
55      Logger localLogger = instanceObject == null ? logger : LoggerFactory.getLogger(instanceObject.getClass());
56      CpoAttribute cpoAttribute = getCpoAttribute();
57      Object param = transformOut(cpoAttribute.invokeGetter(instanceObject));
58     CassandraMethodMapEntry<?,?> methodMapEntry = CassandraMethodMapper.getDatasourceMethod(getDataSetterParamType());
59      if (methodMapEntry == null) {
60        throw new CpoException("Error Retrieveing Cassandra Method for type: " + getDataSetterParamType().getName());
61      }
62      localLogger.info(cpoAttribute.getDataName() + "=" + param);
63      try {
64        switch (methodMapEntry.getMethodType()) {
65          case CassandraMethodMapEntry.METHOD_TYPE_BASIC:
66            methodMapEntry.getBsSetter().invoke(cpoStatementFactory.getBoundStatement(), getIndex(), param);
67            break;
68          case CassandraMethodMapEntry.METHOD_TYPE_ONE:
69            CpoByteArrayInputStream cbais = CpoByteArrayInputStream.getCpoStream((InputStream) param);
70            // Get the length of the InputStream in param
71            methodMapEntry.getBsSetter().invoke(cpoStatementFactory.getBoundStatement(), getIndex(), cbais, cbais.getLength());
72            break;
73          case CassandraMethodMapEntry.METHOD_TYPE_TWO:
74            CpoCharArrayReader ccar = CpoCharArrayReader.getCpoReader((Reader) param);
75            // Get the length of the Reader in param
76            methodMapEntry.getBsSetter().invoke(cpoStatementFactory.getBoundStatement(), getIndex(), ccar, ccar.getLength());
77            break;
78        }
79      } catch (Exception e) {
80        throw new CpoException("Error Invoking Cassandra Method: " + methodMapEntry.getBsSetter().getName() + ":" + ExceptionHelper.getLocalizedMessage(e));
81      }
82    }
83  
84    @Override
85    public Object transformOut(Object attributeObject) throws CpoException {
86      Object retObj = attributeObject;
87      CpoTransform cpoTransform = getCpoAttribute().getCpoTransform();
88  
89      if (cpoTransform != null) {
90        if (cpoTransform instanceof CassandraCpoTransform) {
91          retObj = ((CassandraCpoTransform)cpoTransform).transformOut(cpoStatementFactory, attributeObject);
92        } else {
93          retObj = cpoTransform.transformOut(attributeObject);
94        }
95      }
96      return retObj;
97    }
98  
99  }