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: DefaultSequenceMapper.java 2247 2010-06-29 08:01:42Z schulte2005 $
033 *
034 */
035 // </editor-fold>
036 // SECTION-END
037 package org.jomc.sequences.ri;
038
039 import java.lang.reflect.Field;
040 import java.security.AccessController;
041 import java.security.PrivilegedAction;
042 import java.util.Calendar;
043 import java.util.TimeZone;
044 import org.jomc.sequences.Sequence;
045 import org.jomc.sequences.SequencesSystemException;
046 import org.jomc.sequences.model.SequenceType;
047
048 // SECTION-START[Documentation]
049 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
050 /**
051 * SequenceMapper reference implementation.
052 * <p><b>Specifications</b><ul>
053 * <li>{@code 'org.jomc.sequences.ri.SequenceMapper'} {@code (org.jomc.sequences.ri.SequenceMapper)} {@code 1.0} {@code Multiton}</li>
054 * </ul></p>
055 * <p><b>Dependencies</b><ul>
056 * <li>"{@link #getLocale Locale}"<blockquote>
057 * Dependency on {@code 'java.util.Locale'} {@code (java.util.Locale)} at specification level 1.1 bound to an instance.</blockquote></li>
058 * </ul></p>
059 * <p><b>Messages</b><ul>
060 * <li>"{@link #getIllegalArgumentMessage illegalArgumentMessage}"<table>
061 * <tr><td valign="top">English:</td><td valign="top"><pre>Illegal value ''{1}'' for argument ''{0}''.</pre></td></tr>
062 * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Ungültiger Wert ''{1}'' für Parameter ''{0}''.</pre></td></tr>
063 * </table>
064 * <li>"{@link #getUnhandledExceptionMessage unhandledExceptionMessage}"<table>
065 * <tr><td valign="top">English:</td><td valign="top"><pre>Unhandled exception.</pre></td></tr>
066 * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Unbehandelte Ausnahme.</pre></td></tr>
067 * </table>
068 * </ul></p>
069 *
070 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
071 * @version $Id: DefaultSequenceMapper.java 2247 2010-06-29 08:01:42Z schulte2005 $
072 */
073 // </editor-fold>
074 // SECTION-END
075 // SECTION-START[Annotations]
076 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
077 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
078 // </editor-fold>
079 // SECTION-END
080 public class DefaultSequenceMapper implements SequenceMapper
081 {
082 // SECTION-START[SequenceMapper]
083
084 public Sequence map( final SequenceType sequenceType, final Sequence sequence )
085 {
086 if ( sequenceType == null )
087 {
088 throw new SequencesSystemException( this.getIllegalArgumentMessage(
089 this.getLocale(), "sequenceType", null ) );
090
091 }
092 if ( sequence == null )
093 {
094 throw new SequencesSystemException( this.getIllegalArgumentMessage( this.getLocale(), "sequence", null ) );
095 }
096
097 sequence.setIncrement( sequenceType.getIncrement() );
098 sequence.setMaximum( sequenceType.getMaximum() );
099 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ültiger Wert ''{1}'' fü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 }