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: DefaultSequenceMapper.java 2247 2010-06-29 08:01:42Z schulte2005 $
33   *
34   */
35  // </editor-fold>
36  // SECTION-END
37  package org.jomc.sequences.ri;
38  
39  import java.lang.reflect.Field;
40  import java.security.AccessController;
41  import java.security.PrivilegedAction;
42  import java.util.Calendar;
43  import java.util.TimeZone;
44  import org.jomc.sequences.Sequence;
45  import org.jomc.sequences.SequencesSystemException;
46  import org.jomc.sequences.model.SequenceType;
47  
48  // SECTION-START[Documentation]
49  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
50  /**
51   * SequenceMapper reference implementation.
52   * <p><b>Specifications</b><ul>
53   * <li>{@code 'org.jomc.sequences.ri.SequenceMapper'} {@code (org.jomc.sequences.ri.SequenceMapper)} {@code 1.0} {@code Multiton}</li>
54   * </ul></p>
55   * <p><b>Dependencies</b><ul>
56   * <li>"{@link #getLocale Locale}"<blockquote>
57   * Dependency on {@code 'java.util.Locale'} {@code (java.util.Locale)} at specification level 1.1 bound to an instance.</blockquote></li>
58   * </ul></p>
59   * <p><b>Messages</b><ul>
60   * <li>"{@link #getIllegalArgumentMessage illegalArgumentMessage}"<table>
61   * <tr><td valign="top">English:</td><td valign="top"><pre>Illegal value ''{1}'' for argument ''{0}''.</pre></td></tr>
62   * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Ung&uuml;ltiger Wert ''{1}'' f&uuml;r Parameter ''{0}''.</pre></td></tr>
63   * </table>
64   * <li>"{@link #getUnhandledExceptionMessage unhandledExceptionMessage}"<table>
65   * <tr><td valign="top">English:</td><td valign="top"><pre>Unhandled exception.</pre></td></tr>
66   * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Unbehandelte Ausnahme.</pre></td></tr>
67   * </table>
68   * </ul></p>
69   *
70   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
71   * @version $Id: DefaultSequenceMapper.java 2247 2010-06-29 08:01:42Z schulte2005 $
72   */
73  // </editor-fold>
74  // SECTION-END
75  // SECTION-START[Annotations]
76  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
77  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
78  // </editor-fold>
79  // SECTION-END
80  public class DefaultSequenceMapper implements SequenceMapper
81  {
82      // SECTION-START[SequenceMapper]
83  
84      public Sequence map( final SequenceType sequenceType, final Sequence sequence )
85      {
86          if ( sequenceType == null )
87          {
88              throw new SequencesSystemException( this.getIllegalArgumentMessage(
89                  this.getLocale(), "sequenceType", null ) );
90  
91          }
92          if ( sequence == null )
93          {
94              throw new SequencesSystemException( this.getIllegalArgumentMessage( this.getLocale(), "sequence", null ) );
95          }
96  
97          sequence.setIncrement( sequenceType.getIncrement() );
98          sequence.setMaximum( sequenceType.getMaximum() );
99          sequence.setMinimum( sequenceType.getMinimum() );
100         sequence.setName( sequenceType.getName() );
101         sequence.setValue( sequenceType.getValue() );
102         this.injectRevision( sequence, sequenceType.getRevision() );
103         this.injectDate( sequence, sequenceType.getJpaDate().getTimeInMillis() );
104         return sequence;
105     }
106 
107     public SequenceType map( final Sequence sequence, final SequenceType sequenceType )
108     {
109         if ( sequence == null )
110         {
111             throw new SequencesSystemException( this.getIllegalArgumentMessage( this.getLocale(), "sequence", null ) );
112         }
113         if ( sequenceType == null )
114         {
115             throw new SequencesSystemException( this.getIllegalArgumentMessage(
116                 this.getLocale(), "sequenceType", null ) );
117 
118         }
119 
120         sequenceType.setIncrement( sequence.getIncrement() );
121         sequenceType.setMaximum( sequence.getMaximum() );
122         sequenceType.setMinimum( sequence.getMinimum() );
123         sequenceType.setName( sequence.getName() );
124         sequenceType.setValue( sequence.getValue() );
125         sequenceType.setRevision( sequence.getRevision() );
126 
127         final Calendar c = Calendar.getInstance();
128         c.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
129         c.setTimeInMillis( sequence.getDate() );
130         sequenceType.setJpaDate( c );
131 
132         return sequenceType;
133     }
134 
135     // SECTION-END
136     // SECTION-START[DefaultSequenceMapper]
137     /**
138      * Sets the value of property {@code revision} of a given sequence using reflection.
139      *
140      * @param s The sequence to update.
141      * @param value The new value for property {@code revision}.
142      *
143      * @throws SequencesSystemException if injecting {@code value} fails unexpectedly.
144      */
145     protected void injectRevision( final Sequence s, final long value )
146     {
147         this.injectFieldValue( s, "revision", Long.valueOf( value ) );
148     }
149 
150     /**
151      * Sets the value of property {@code date} of a given sequence using reflection.
152      *
153      * @param s The sequence to update.
154      * @param value The new value for property {@code date}.
155      *
156      * @throws SequencesSystemException if injecting {@code value} fails unexpectedly.
157      */
158     protected void injectDate( final Sequence s, final long value )
159     {
160         this.injectFieldValue( s, "date", Long.valueOf( value ) );
161     }
162 
163     /**
164      * Sets the value of a field of a given object using reflection.
165      *
166      * @param object The object to update.
167      * @param fieldName The name of the field to update.
168      * @param value The new value for field {@code fieldName}.
169      *
170      * @throws SequencesSystemException if setting {@code value} fails unexpectedly.
171      */
172     protected void injectFieldValue( final Object object, final String fieldName, final Object value )
173     {
174         AccessController.doPrivileged( new PrivilegedAction<Object>()
175         {
176 
177             public Object run()
178             {
179                 Field field = null;
180 
181                 try
182                 {
183                     field = object.getClass().getDeclaredField( fieldName );
184                     field.setAccessible( true );
185                     field.set( object, value );
186                     field.setAccessible( false );
187                     return null;
188                 }
189                 catch ( final Exception e )
190                 {
191                     throw new SequencesSystemException( getUnhandledExceptionMessage( getLocale() ), e );
192                 }
193             }
194 
195         } );
196     }
197 
198     // SECTION-END
199     // SECTION-START[Constructors]
200     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
201 
202     /** Creates a new {@code DefaultSequenceMapper} instance. */
203     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
204     public DefaultSequenceMapper()
205     {
206         // SECTION-START[Default Constructor]
207         super();
208         // SECTION-END
209     }
210     // </editor-fold>
211     // SECTION-END
212     // SECTION-START[Dependencies]
213     // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
214 
215     /**
216      * Gets the {@code Locale} dependency.
217      * <p>This method returns the {@code 'default'} object of the {@code 'java.util.Locale'} {@code (java.util.Locale)} specification at specification level 1.1.</p>
218      * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
219      * @return The {@code Locale} dependency.
220      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
221      */
222     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
223     private java.util.Locale getLocale()
224     {
225         final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
226         assert _d != null : "'Locale' dependency not found.";
227         return _d;
228     }
229     // </editor-fold>
230     // SECTION-END
231     // SECTION-START[Properties]
232     // SECTION-END
233     // SECTION-START[Messages]
234     // <editor-fold defaultstate="collapsed" desc=" Generated Messages ">
235 
236     /**
237      * Gets the text of the {@code illegalArgumentMessage} message.
238      * <p><b>Templates</b><br/><table>
239      * <tr><td valign="top">English:</td><td valign="top"><pre>Illegal value ''{1}'' for argument ''{0}''.</pre></td></tr>
240      * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Ung&uuml;ltiger Wert ''{1}'' f&uuml;r Parameter ''{0}''.</pre></td></tr>
241      * </table></p>
242      * @param locale The locale of the message to return.
243      * @param argumentName Format argument.
244      * @param argumentValue Format argument.
245      * @return The text of the {@code illegalArgumentMessage} message.
246      *
247      * @throws org.jomc.ObjectManagementException if getting the message instance fails.
248      */
249     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
250     private String getIllegalArgumentMessage( final java.util.Locale locale, final java.lang.String argumentName, final java.lang.String argumentValue )
251     {
252         final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "illegalArgumentMessage", locale, argumentName, argumentValue );
253         assert _m != null : "'illegalArgumentMessage' message not found.";
254         return _m;
255     }
256 
257     /**
258      * Gets the text of the {@code unhandledExceptionMessage} message.
259      * <p><b>Templates</b><br/><table>
260      * <tr><td valign="top">English:</td><td valign="top"><pre>Unhandled exception.</pre></td></tr>
261      * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Unbehandelte Ausnahme.</pre></td></tr>
262      * </table></p>
263      * @param locale The locale of the message to return.
264      * @return The text of the {@code unhandledExceptionMessage} message.
265      *
266      * @throws org.jomc.ObjectManagementException if getting the message instance fails.
267      */
268     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
269     private String getUnhandledExceptionMessage( final java.util.Locale locale )
270     {
271         final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "unhandledExceptionMessage", locale );
272         assert _m != null : "'unhandledExceptionMessage' message not found.";
273         return _m;
274     }
275     // </editor-fold>
276     // SECTION-END
277 }