001 // SECTION-START[License Header]
002 // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003 /*
004 * Copyright (c) 2010 The JOMC Project
005 * Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
006 * All rights reserved.
007 *
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions
010 * are met:
011 *
012 * o Redistributions of source code must retain the above copyright
013 * notice, this list of conditions and the following disclaimer.
014 *
015 * o Redistributions in binary form must reproduce the above copyright
016 * notice, this list of conditions and the following disclaimer in
017 * the documentation and/or other materials provided with the
018 * distribution.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 *
032 * $Id: NativeQueryFactory.java 2236 2010-06-29 00:05:34Z schulte2005 $
033 *
034 */
035 // </editor-fold>
036 // SECTION-END
037 package org.jomc.sdk.jpa;
038
039 import java.util.Map;
040 import javax.persistence.EntityManager;
041 import javax.persistence.Query;
042
043 // SECTION-START[Documentation]
044 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
045 /**
046 * Factory providing a native JPA query instance.
047 * <p><b>Specifications</b><ul>
048 * <li>{@code 'javax.persistence.Query'} {@code (javax.persistence.Query)} {@code Multiton}</li>
049 * </ul></p>
050 * <p><b>Properties</b><ul>
051 * <li>"{@link #getNativeQuery nativeQuery}"
052 * <blockquote>Property of type {@code java.lang.String}.
053 * <p>Native query to provide.</p>
054 * </blockquote></li>
055 * <li>"{@link #getParameterMap parameterMap}"
056 * <blockquote>Property of type {@code java.util.Map<String,Object>}.
057 * <p>Map of parameters to set on the query.</p>
058 * </blockquote></li>
059 * </ul></p>
060 * <p><b>Dependencies</b><ul>
061 * <li>"{@link #getEntityManager EntityManager}"<blockquote>
062 * Dependency on {@code 'javax.persistence.EntityManager'} {@code (javax.persistence.EntityManager)}.</blockquote></li>
063 * </ul></p>
064 *
065 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
066 * @version $Id: NativeQueryFactory.java 2236 2010-06-29 00:05:34Z schulte2005 $
067 */
068 // </editor-fold>
069 // SECTION-END
070 // SECTION-START[Annotations]
071 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
072 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
073 // </editor-fold>
074 // SECTION-END
075 public final class NativeQueryFactory
076 {
077 // SECTION-START[Query]
078 // SECTION-END
079 // SECTION-START[NativeQueryFactory]
080
081 public Query getObject()
082 {
083 final EntityManager em = this.getEntityManager();
084
085 if ( em != null )
086 {
087 final Query q = em.createNativeQuery( this.getNativeQuery() );
088
089 for ( Map.Entry<String, Object> e : this.getParameterMap().entrySet() )
090 {
091 q.setParameter( e.getKey(), e.getValue() );
092 }
093
094 return q;
095 }
096
097 return null;
098 }
099
100 // SECTION-END
101 // SECTION-START[Constructors]
102 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
103
104 /** Creates a new {@code NativeQueryFactory} 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 NativeQueryFactory()
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 nativeQuery} property.
137 * @return Native query to provide.
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.lang.String getNativeQuery()
142 {
143 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "nativeQuery" );
144 assert _p != null : "'nativeQuery' property not found.";
145 return _p;
146 }
147
148 /**
149 * Gets the value of the {@code parameterMap} property.
150 * @return Map of parameters to set on the query.
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.util.Map<String,Object> getParameterMap()
155 {
156 final java.util.Map<String,Object> _p = (java.util.Map<String,Object>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "parameterMap" );
157 assert _p != null : "'parameterMap' property not found.";
158 return _p;
159 }
160 // </editor-fold>
161 // SECTION-END
162 // SECTION-START[Messages]
163 // SECTION-END
164 }