View Javadoc

1   // SECTION-START[License Header]
2   // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3   /*
4    *   Copyright (c) 2010 The JOMC Project
5    *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
6    *   All rights reserved.
7    *
8    *   Redistribution and use in source and binary forms, with or without
9    *   modification, are permitted provided that the following conditions
10   *   are met:
11   *
12   *     o Redistributions of source code must retain the above copyright
13   *       notice, this list of conditions and the following disclaimer.
14   *
15   *     o Redistributions in binary form must reproduce the above copyright
16   *       notice, this list of conditions and the following disclaimer in
17   *       the documentation and/or other materials provided with the
18   *       distribution.
19   *
20   *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
21   *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
24   *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25   *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26   *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27   *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29   *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30   *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31   *
32   *   $Id: QueryFactory.java 2236 2010-06-29 00:05:34Z schulte2005 $
33   *
34   */
35  // </editor-fold>
36  // SECTION-END
37  package org.jomc.sdk.jpa;
38  
39  import java.util.Map;
40  import javax.persistence.EntityManager;
41  import javax.persistence.Query;
42  
43  // SECTION-START[Documentation]
44  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
45  /**
46   * Factory providing a JPA query instance.
47   * <p><b>Specifications</b><ul>
48   * <li>{@code 'javax.persistence.Query'} {@code (javax.persistence.Query)} {@code Multiton}</li>
49   * </ul></p>
50   * <p><b>Properties</b><ul>
51   * <li>"{@link #getParameterMap parameterMap}"
52   * <blockquote>Property of type {@code java.util.Map<String,Object>}.
53   * <p>Map of parameters to set on the query.</p>
54   * </blockquote></li>
55   * <li>"{@link #getQuery query}"
56   * <blockquote>Property of type {@code java.lang.String}.
57   * <p>Query to provide.</p>
58   * </blockquote></li>
59   * </ul></p>
60   * <p><b>Dependencies</b><ul>
61   * <li>"{@link #getEntityManager EntityManager}"<blockquote>
62   * Dependency on {@code 'javax.persistence.EntityManager'} {@code (javax.persistence.EntityManager)}.</blockquote></li>
63   * </ul></p>
64   *
65   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
66   * @version $Id: QueryFactory.java 2236 2010-06-29 00:05:34Z schulte2005 $
67   */
68  // </editor-fold>
69  // SECTION-END
70  // SECTION-START[Annotations]
71  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
72  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
73  // </editor-fold>
74  // SECTION-END
75  public final class QueryFactory
76  {
77      // SECTION-START[Query]
78      // SECTION-END
79      // SECTION-START[QueryFactory]
80  
81      public Query getObject()
82      {
83          final EntityManager em = this.getEntityManager();
84  
85          if ( em != null )
86          {
87              final Query q = em.createQuery( this.getQuery() );
88  
89              for ( Map.Entry<String, Object> e : this.getParameterMap().entrySet() )
90              {
91                  q.setParameter( e.getKey(), e.getValue() );
92              }
93  
94              return q;
95          }
96  
97          return null;
98      }
99  
100     // SECTION-END
101     // SECTION-START[Constructors]
102     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
103 
104     /** Creates a new {@code QueryFactory} instance. */
105     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
106     public QueryFactory()
107     {
108         // SECTION-START[Default Constructor]
109         super();
110         // SECTION-END
111     }
112     // </editor-fold>
113     // SECTION-END
114     // SECTION-START[Dependencies]
115     // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
116 
117     /**
118      * Gets the {@code EntityManager} dependency.
119      * <p>This method returns the {@code 'JOMC SDK JPA'} object of the {@code 'javax.persistence.EntityManager'} {@code (javax.persistence.EntityManager)} specification.</p>
120      * <p>That specification does not apply to any scope. A new object is returned whenever requested.</p>
121      * @return The {@code EntityManager} dependency.
122      * {@code null} if no object is available.
123      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
124      */
125     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
126     private javax.persistence.EntityManager getEntityManager()
127     {
128         return (javax.persistence.EntityManager) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "EntityManager" );
129     }
130     // </editor-fold>
131     // SECTION-END
132     // SECTION-START[Properties]
133     // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
134 
135     /**
136      * Gets the value of the {@code parameterMap} property.
137      * @return Map of parameters to set on the query.
138      * @throws org.jomc.ObjectManagementException if getting the property instance fails.
139      */
140     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
141     private java.util.Map<String,Object> getParameterMap()
142     {
143         final java.util.Map<String,Object> _p = (java.util.Map<String,Object>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "parameterMap" );
144         assert _p != null : "'parameterMap' property not found.";
145         return _p;
146     }
147 
148     /**
149      * Gets the value of the {@code query} property.
150      * @return Query to provide.
151      * @throws org.jomc.ObjectManagementException if getting the property instance fails.
152      */
153     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
154     private java.lang.String getQuery()
155     {
156         final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "query" );
157         assert _p != null : "'query' property not found.";
158         return _p;
159     }
160     // </editor-fold>
161     // SECTION-END
162     // SECTION-START[Messages]
163     // SECTION-END
164 }