View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.config.transfer.jdbc;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  import de.smartics.properties.api.config.transfer.PropertySinkFactory;
21  import de.smartics.properties.impl.config.ds.DataSourceProxyManager;
22  import de.smartics.properties.spi.config.ds.DataSourceConnector;
23  import de.smartics.properties.spi.config.ds.DataSourceProxy;
24  import de.smartics.properties.spi.config.ds.PropertiesStore;
25  
26  /**
27   * Creates instances of {@link JdbcPropertySink}.
28   */
29  public final class Factory implements PropertySinkFactory<JdbcPropertySink>
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    /**
38     * The database identifier to select the datasource proxy for a database
39     * product.
40     */
41    private String databaseId;
42  
43    /**
44     * The JDBC URL to the database.
45     */
46    private String jdbcUrl;
47  
48    /**
49     * The user to authenticate against the database.
50     */
51    private String user;
52  
53    /**
54     * The password to authenticate against the database.
55     */
56    private String password;
57  
58    /**
59     * The flag that signals to drop properties table (<code>true</code>) or not (
60     * <code>false</code>).
61     */
62    private boolean dropTable;
63  
64    /**
65     * The flag that signals to create properties table (<code>true</code>) or not
66     * ( <code>false</code>).
67     */
68    private boolean createTable;
69  
70    // ****************************** Initializer *******************************
71  
72    // ****************************** Constructors ******************************
73  
74    /**
75     * Default constructor.
76     */
77    public Factory()
78    {
79    }
80  
81    // ****************************** Inner Classes *****************************
82  
83    // ********************************* Methods ********************************
84  
85    // --- init -----------------------------------------------------------------
86  
87    // --- get&set --------------------------------------------------------------
88  
89    /**
90     * Returns the database identifier to select the datasource proxy for a
91     * database product.
92     *
93     * @return the database identifier to select the datasource proxy for a
94     *         database product.
95     */
96    public String getDatabaseId()
97    {
98      return databaseId;
99    }
100 
101   /**
102    * Sets the database identifier to select the datasource proxy for a database
103    * product.
104    *
105    * @param databaseId the database identifier to select the datasource proxy
106    *          for a database product.
107    */
108   public void setDatabaseId(final String databaseId)
109   {
110     this.databaseId = databaseId;
111   }
112 
113   /**
114    * Returns the JDBC URL to the database.
115    *
116    * @return the JDBC URL to the database.
117    */
118   public String getJdbcUrl()
119   {
120     return jdbcUrl;
121   }
122 
123   /**
124    * Sets the JDBC URL to the database.
125    *
126    * @param jdbcUrl the JDBC URL to the database.
127    */
128   public void setJdbcUrl(final String jdbcUrl)
129   {
130     this.jdbcUrl = jdbcUrl;
131   }
132 
133   /**
134    * Returns the user to authenticate against the database.
135    *
136    * @return the user to authenticate against the database.
137    */
138   public String getUser()
139   {
140     return user;
141   }
142 
143   /**
144    * Sets the user to authenticate against the database.
145    *
146    * @param user the user to authenticate against the database.
147    */
148   public void setUser(final String user)
149   {
150     this.user = user;
151   }
152 
153   /**
154    * Returns the password to authenticate against the database.
155    *
156    * @return the password to authenticate against the database.
157    */
158   public String getPassword()
159   {
160     return password;
161   }
162 
163   /**
164    * Sets the password to authenticate against the database.
165    *
166    * @param password the password to authenticate against the database.
167    */
168   public void setPassword(final String password)
169   {
170     this.password = password;
171   }
172 
173   /**
174    * Returns the flag that signals to drop properties table (<code>true</code>)
175    * or not ( <code>false</code>).
176    *
177    * @return the flag that signals to drop properties table (<code>true</code>)
178    *         or not ( <code>false</code>).
179    */
180   public boolean isDropTable()
181   {
182     return dropTable;
183   }
184 
185   /**
186    * Sets the flag that signals to drop properties table (<code>true</code>) or
187    * not ( <code>false</code>).
188    *
189    * @param dropTable the flag that signals to drop properties table (
190    *          <code>true</code>) or not ( <code>false</code>).
191    */
192   public void setDropTable(final boolean dropTable)
193   {
194     this.dropTable = dropTable;
195   }
196 
197   /**
198    * Returns the flag that signals to create properties table (<code>true</code>
199    * ) or not ( <code>false</code>).
200    *
201    * @return the flag that signals to create properties table (<code>true</code>
202    *         ) or not ( <code>false</code>).
203    */
204   public boolean isCreateTable()
205   {
206     return createTable;
207   }
208 
209   /**
210    * Sets the flag that signals to create properties table (<code>true</code>)
211    * or not ( <code>false</code>).
212    *
213    * @param createTable the flag that signals to create properties table (
214    *          <code>true</code>) or not ( <code>false</code>).
215    */
216   public void setCreateTable(final boolean createTable)
217   {
218     this.createTable = createTable;
219   }
220 
221   // --- business -------------------------------------------------------------
222 
223   @Override
224   public JdbcPropertySink create()
225   {
226     final PropertiesStore store = createManager();
227     return new JdbcPropertySink(store);
228   }
229 
230   private PropertiesStore createManager()
231   {
232     final PropertiesStore.Builder builder = new PropertiesStore.Builder();
233 
234     final DataSourceConnector connector =
235         DataSourceProxyManager.getConnectorFor(databaseId);
236     final DataSourceProxy dataSourceProxy =
237         connector.create(jdbcUrl, user, password);
238     builder.setDataSourceProxy(dataSourceProxy);
239 
240     builder.setDropTable(dropTable);
241     builder.setIgnoreTableCreationProblems(false);
242 
243     final String createTableSqlStatementTemplate =
244         dataSourceProxy.getCreateTableSqlTemplate();
245     if (StringUtils.isNotBlank(createTableSqlStatementTemplate))
246     {
247       builder
248           .setCreateTableSqlStatementTemplate(createTableSqlStatementTemplate);
249     }
250 
251     final String insertOrUpdateSqlTemplate =
252         dataSourceProxy.getInsertOrUpdateSqlTemplate();
253     if (StringUtils.isNotBlank(insertOrUpdateSqlTemplate))
254     {
255       builder
256           .setInsertOrUpdateSqlStatementTemplate(insertOrUpdateSqlTemplate);
257     }
258 
259     final PropertiesStore manager = builder.build();
260 
261     if (createTable)
262     {
263       manager.createConfigTable();
264     }
265 
266     return manager;
267   }
268 
269   // --- object basics --------------------------------------------------------
270 
271 }