View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 pw999
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 net.sf.oqt.jpa;
17  
18  import java.math.BigInteger;
19  import java.util.Date;
20  
21  /**
22   * Factory to initialize whathever has to be initialized to allows proper JPQL execution.
23   * 
24   * @author phillip
25   * 
26   */
27  class ParameterFactory {
28      /**
29       * Tries to initialized an object of type clazz.
30       * 
31       * @param clazz the class to initliase.
32       * @return the initialized object or an empty String if object could ne be initialized.
33       */
34      static Object intialize(final Class<?> clazz) {
35          if (clazz.equals(Object.class)) { // when using IN statements
36              return "";
37          }
38          try {
39              return clazz.newInstance();
40          } catch (IllegalAccessException e) {
41              ; // it failed,let's do it manually
42          } catch (InstantiationException e) {
43              ; // it failed,let's do it manually
44          }
45          if (clazz.equals(Long.class)) {
46              return 1L;
47          }
48          if (clazz.equals(Integer.class)) {
49              return 1;
50          }
51          if (clazz.equals(Short.class)) {
52              return Short.valueOf("1");
53          }
54          if (clazz.equals(BigInteger.class)) {
55              return BigInteger.ONE;
56          }
57          if (clazz.equals(String.class)) {
58              return "1";
59          }
60          if (clazz.equals(Date.class)) {
61              return new Date();
62          }
63          if (clazz.equals(Boolean.class)) {
64              return true;
65          }
66          System.out.println("Unknown parameter type, I'll try a string");
67          return "";
68      }
69  }