View Javadoc
1   package org.synchronoss.cpo.jdbc;
2   
3   /*-
4    * [[
5    * jdbc
6    * ==
7    * Copyright (C) 2003 - 2026 Exaxis LLC, Synchronoss Technologies Inc
8    * ==
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ]]
23   */
24  
25  import java.lang.reflect.InvocationTargetException;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.synchronoss.cpo.core.CpoException;
29  import org.synchronoss.cpo.core.meta.domain.CpoAttribute;
30  import org.synchronoss.cpo.core.transform.CpoTransform;
31  import org.synchronoss.cpo.jdbc.meta.JdbcMethodMapEntry;
32  import org.synchronoss.cpo.jdbc.transform.JdbcCpoTransform;
33  
34  /**
35   * The data handler for a prepared statement
36   *
37   * @author dberry
38   */
39  public class JdbcPreparedStatementCpoData extends AbstractStatementCpoData {
40  
41    private static final Logger logger = LoggerFactory.getLogger(JdbcPreparedStatementCpoData.class);
42    private JdbcPreparedStatementFactory cpoStatementFactory = null;
43  
44    /**
45     * Construct a JdbcPreparedStatementCpoData
46     *
47     * @param cpoStatementFactory - The JdbcPreparedStatementFactory
48     * @param cpoAttribute - The CpoAttribute to manage
49     * @param index - The index of the CpoAttribute
50     */
51    public JdbcPreparedStatementCpoData(
52        JdbcPreparedStatementFactory cpoStatementFactory, CpoAttribute cpoAttribute, int index) {
53      super(cpoAttribute, index);
54      this.cpoStatementFactory = cpoStatementFactory;
55    }
56  
57    @Override
58    public void invokeSetter(Object instanceObject) throws CpoException {
59      JdbcMethodMapEntry<?, ?> methodMapEntry = getJdbcMethodMapEntry(instanceObject);
60      Logger localLogger =
61          instanceObject == null ? logger : LoggerFactory.getLogger(instanceObject.getClass());
62      try {
63        Object param = transformOut(getCpoAttribute().invokeGetter(instanceObject));
64        localLogger.debug("{}={}", getCpoAttribute().getDataName(), param);
65        methodMapEntry
66            .getBsSetter()
67            .invoke(cpoStatementFactory.getPreparedStatement(), getIndex(), param);
68      } catch (InvocationTargetException | IllegalAccessException e) {
69        throw new CpoException(
70            "Error Invoking Jdbc Method: "
71                + getCpoAttribute().getDataName()
72                + ":"
73                + getCpoAttribute().getJavaName()
74                + ":"
75                + methodMapEntry.getBsSetter().getName()
76                + ":",
77            e);
78      }
79    }
80  
81    @Override
82    public Object transformOut(Object attributeObject) throws CpoException {
83      Object retObj = attributeObject;
84      CpoTransform cpoTransform = getCpoAttribute().getCpoTransform();
85  
86      if (cpoTransform != null) {
87        if (cpoTransform instanceof JdbcCpoTransform) {
88          retObj =
89              ((JdbcCpoTransform) cpoTransform).transformOut(cpoStatementFactory, attributeObject);
90        } else {
91          retObj = cpoTransform.transformOut(attributeObject);
92        }
93      }
94      return retObj;
95    }
96  }