From: <tri...@us...> - 2007-09-20 12:24:54
|
Revision: 90 http://equanda.svn.sourceforge.net/equanda/?rev=90&view=rev Author: triathlon98 Date: 2007-09-20 05:24:52 -0700 (Thu, 20 Sep 2007) Log Message: ----------- EQ-5 refactor FieldHandler usages to have less duplication Modified Paths: -------------- trunk/equanda-generate/src/main/velocity/import/FieldMap.java.vm trunk/equanda-generate/src/main/velocity/import/ImportObjectProxy.java.vm trunk/equanda-generate/src/main/velocity/import/TableHandler.java.vm trunk/equanda-server/src/main/java/org/equanda/ymport/FieldHandler.java trunk/equanda-server/src/main/java/org/equanda/ymport/FieldMapAdapter.java Added Paths: ----------- trunk/equanda-client/src/main/java/org/equanda/persistence/om/EquandaImportProxy.java trunk/equanda-server/src/main/java/org/equanda/ymport/FieldParser.java Added: trunk/equanda-client/src/main/java/org/equanda/persistence/om/EquandaImportProxy.java =================================================================== --- trunk/equanda-client/src/main/java/org/equanda/persistence/om/EquandaImportProxy.java (rev 0) +++ trunk/equanda-client/src/main/java/org/equanda/persistence/om/EquandaImportProxy.java 2007-09-20 12:24:52 UTC (rev 90) @@ -0,0 +1,74 @@ +/** + * This file is part of the equanda project. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + */ + +package org.equanda.persistence.om; + +/** + * Some extra mathods which are available in all importproxy objects + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public interface EquandaImportProxy +{ + void setUOID( UOID id ); + + void setUOID( String id ); + + boolean hasUOID(); + + String getEquandaType(); + + public UOID getUOID(); + + java.sql.Timestamp getEquandaModificationDate(); + + long getEquandaVersion(); + + boolean isEquandaModificationDateModified(); + + String getEquandaStatus(); + + void setEquandaStatus( String status ); + + String getEquandaName(); + + boolean isEquandaType( String type ); + + void setEquandaType( String type ); + + boolean isModified( boolean typeFieldsOnly ); + + boolean isModified(); + + void equandaUpdate() + throws EquandaPersistenceException; + + void equandaUpdate( boolean updateTypeFieldsOnly ) + throws EquandaPersistenceException; + + void removeEntityBean() + throws EquandaPersistenceException; + + void setEquandaUpdate( boolean update ); +} Modified: trunk/equanda-generate/src/main/velocity/import/FieldMap.java.vm =================================================================== --- trunk/equanda-generate/src/main/velocity/import/FieldMap.java.vm 2007-09-19 16:05:37 UTC (rev 89) +++ trunk/equanda-generate/src/main/velocity/import/FieldMap.java.vm 2007-09-20 12:24:52 UTC (rev 90) @@ -27,13 +27,17 @@ package $package; -import org.equanda.ymport.*; +import org.equanda.persistence.om.EquandaImportProxy; +import org.equanda.ymport.FieldHandler; +import org.equanda.ymport.FieldMapAdapter; +import org.equanda.ymport.FieldParser; +import org.equanda.ymport.ImportHandlerException; +import org.equanda.ymport.ImportParser; + import java.util.Collection; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; -import org.equanda.persistence.om.UOID; -import org.equanda.persistence.om.EquandaPersistenceException; #set( $ejbPackage=${engine.getConfig("ejb-package")} ) import ${ejbPackage}.client.*; @@ -43,36 +47,18 @@ class ${table.Name}FieldMap extends FieldMapAdapter { /** map object, for quick and easy retrieval of FieldHandler */ - private static HashMap fieldsMap = new HashMap(); + private static HashMap<String, FieldHandler> fieldsMap = new HashMap<String, FieldHandler>(); /** map object, for quick and easy retrieval Table type for a linked field */ - private static HashMap tablesMap = new HashMap(); + private static HashMap<String, String> tablesMap = new HashMap<String, String>(); - protected HashMap getFieldsMap() { return fieldsMap; } - protected HashMap getTablesMap() { return tablesMap; } + protected HashMap<String, FieldHandler> getFieldsMap() { return fieldsMap; } + protected HashMap<String, String> getTablesMap() { return tablesMap; } static { - fieldsMap.put( "UOID", - new FieldHandler() - { - public void setField( Object proxyObject, Object fieldValue ) - throws ImportHandlerException - { - Import${table.Name} obj = (Import${table.Name}) proxyObject; - obj.setUOID( ImportParser.parseString( fieldValue ) ); - } - } ); - fieldsMap.put( "equandaType", - new FieldHandler() - { - public void setField( Object proxyObject, Object fieldValue ) - throws ImportHandlerException - { - Import${table.Name} obj = (Import${table.Name}) proxyObject; - obj.setEquandaType( ImportParser.parseString( fieldValue ) ); - } - } ); + fieldsMap.put( "UOID", FieldHandler.UOID ); + fieldsMap.put( "equandaType", FieldHandler.EquandaType ); #foreach( $field in $table.Fields ) #if( !$field.isAuto() && !$field.isInternal() && !$field.isValueExclude() && !$field.isCalculated() ) @@ -80,64 +66,46 @@ #if( $field.isLink() ) #set( $suffix = "//" ) #end - fieldsMap.put("$field.Name$suffix", new FieldHandler() + fieldsMap.put("$field.Name$suffix", +#if( !$field.isMultiple() ) + new FieldHandler() { public void setField( Object proxyObject, Object fieldValue ) throws ImportHandlerException { Import${table.Name} obj = (Import${table.Name}) proxyObject; - #if( !$field.isMultiple() ) - #if( !$field.isLink() ) - obj.set${field.Name}( ImportParser.parse${field.FieldTypeName}( fieldValue ) ); - #else - ## linked field - if ( fieldValue == null ) - { - obj.set${field.Name}( null ); - } - else - { - obj.set${field.Name}( (${field.LinkTable.Name}) fieldValue ); - } - #end + #if( !$field.isLink() ) + obj.set${field.Name}( ImportParser.parse${field.FieldTypeName}( fieldValue ) ); #else - ## multiple field - Collection value; - #if( !$field.isLink() ) - ## normal field with multiple values - value = new ArrayList(); - if ( fieldValue instanceof Collection ) - { - Iterator it = ( (Collection) fieldValue ).iterator(); - while ( it.hasNext() ) - { - value.add( ImportParser.parse${field.FieldTypeName}( it.next() ) ); - } - } - else - { - value.add( ImportParser.parse${field.FieldTypeName}( fieldValue ) ); - } - #else - ## linked field - if ( fieldValue instanceof Collection ) - { - value = (Collection) fieldValue; - } - else - { - value = new ArrayList(); - value.add( fieldValue ); - } - #end - obj.set${field.Name}( value ); + ## linked field + if ( fieldValue == null ) + { + obj.set${field.Name}( null ); + } + else + { + obj.set${field.Name}( (${field.LinkTable.Name}) fieldValue ); + } #end } } ); +#else##multiple +#set( $par = "") +#if( !$field.isLink() ) +#set( $par = " FieldParser.parse${field.FieldTypeName} " ) +#end + new FieldHandler($par) + { + public void setMultiple( EquandaImportProxy proxy, Collection value ) + { + ((Import${table.Name}) proxy).set${field.Name}( value ); + } + } ); +#end##multiple #end ## if importable field #end ## foreach field - tablesMap = new HashMap(); + tablesMap = new HashMap<String,String>(); #foreach( $field in $table.Fields ) #if( $field.isLink() && !$field.isAuto() && !$field.isInternal() && !$field.isValueExclude() && !$field.isCalculated() ) Modified: trunk/equanda-generate/src/main/velocity/import/ImportObjectProxy.java.vm =================================================================== --- trunk/equanda-generate/src/main/velocity/import/ImportObjectProxy.java.vm 2007-09-19 16:05:37 UTC (rev 89) +++ trunk/equanda-generate/src/main/velocity/import/ImportObjectProxy.java.vm 2007-09-20 12:24:52 UTC (rev 90) @@ -30,9 +30,12 @@ #set( $ejbPackage=${engine.getConfig("ejb-package")} ) import ${ejbPackage}.client.*; import org.equanda.persistence.om.UOID; +import org.equanda.persistence.om.EquandaImportProxy; import java.util.Collection; -class Import${table.Name} extends ${ejbPackage}.client.${table.Name} +class Import${table.Name} + extends ${ejbPackage}.client.${table.Name} + implements EquandaImportProxy { #foreach ($field in $table.Fields) Modified: trunk/equanda-generate/src/main/velocity/import/TableHandler.java.vm =================================================================== --- trunk/equanda-generate/src/main/velocity/import/TableHandler.java.vm 2007-09-19 16:05:37 UTC (rev 89) +++ trunk/equanda-generate/src/main/velocity/import/TableHandler.java.vm 2007-09-20 12:24:52 UTC (rev 90) @@ -65,8 +65,12 @@ obj.remove(); return newProxy; } - catch (Exception e) + catch( ImportHandlerException e ) { + throw e; + } + catch( Exception e ) + { throw new ImportHandlerException( e ); } finally Modified: trunk/equanda-server/src/main/java/org/equanda/ymport/FieldHandler.java =================================================================== --- trunk/equanda-server/src/main/java/org/equanda/ymport/FieldHandler.java 2007-09-19 16:05:37 UTC (rev 89) +++ trunk/equanda-server/src/main/java/org/equanda/ymport/FieldHandler.java 2007-09-20 12:24:52 UTC (rev 90) @@ -24,21 +24,91 @@ package org.equanda.ymport; +import org.equanda.persistence.om.EquandaImportProxy; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + /** * Field handler, adds an extra field in a value object * * @author NetRom team */ -public interface FieldHandler +public class FieldHandler { + public static final FieldHandler UOID = new FieldHandler() + { + public void setField( Object proxyObject, Object fieldValue ) + throws ImportHandlerException + { + EquandaImportProxy obj = (EquandaImportProxy) proxyObject; + obj.setUOID( ImportParser.parseString( fieldValue ) ); + } + }; + public static final FieldHandler EquandaType = new FieldHandler() + { + public void setField( Object proxyObject, Object fieldValue ) + throws ImportHandlerException + { + EquandaImportProxy obj = (EquandaImportProxy) proxyObject; + obj.setEquandaType( ImportParser.parseString( fieldValue ) ); + } + }; + + private FieldParser parser; + + public FieldHandler() {} // creator for linked fields + + public FieldHandler( FieldParser parser ) { this.parser = parser; } // crfeator for multiple fields + /** * Set the given fieldValue in the given valueObject * - * @param valueObject value object which needs to have the field set + * @param proxyObject import proxy object which needs to have the field set * @param fieldValue field value to set, as result from parser, should not be null (in that case * the FieldHandler should not be called, as the value was not specified) - * @throws ImportHandlerException + * @throws ImportHandlerException oops */ - void setField( Object valueObject, Object fieldValue ) - throws ImportHandlerException; + public void setField( Object proxyObject, Object fieldValue ) + throws ImportHandlerException + { + // this is the default handling for a multiple field + EquandaImportProxy obj = (EquandaImportProxy) proxyObject; + Collection value; + if ( parser != null ) + { + value = new ArrayList(); + if ( fieldValue instanceof Collection ) + { + Iterator it = ( (Collection) fieldValue ).iterator(); + while ( it.hasNext() ) + { + value.add( parser.parse( it.next() ) ); + } + } + else + { + value.add( parser.parse( fieldValue ) ); + } + } + else + { + if ( fieldValue instanceof Collection ) + { + value = (Collection) fieldValue; + } + else + { + value = new ArrayList(); + value.add( fieldValue ); + } + } + setMultiple( obj, value ); + } + + public void setMultiple( EquandaImportProxy proxy, Collection value ) + { + // to be provided for multiple fields + } } \ No newline at end of file Modified: trunk/equanda-server/src/main/java/org/equanda/ymport/FieldMapAdapter.java =================================================================== --- trunk/equanda-server/src/main/java/org/equanda/ymport/FieldMapAdapter.java 2007-09-19 16:05:37 UTC (rev 89) +++ trunk/equanda-server/src/main/java/org/equanda/ymport/FieldMapAdapter.java 2007-09-20 12:24:52 UTC (rev 90) @@ -39,13 +39,15 @@ { /** * get fieldsMap for this class, to be overwritten + * @return fieldhandler list */ - protected abstract HashMap getFieldsMap(); + protected abstract HashMap<String, FieldHandler> getFieldsMap(); /** * get tablesMap for this class, to be overwritten + * @return fieldhandler list */ - protected abstract HashMap getTablesMap(); + protected abstract HashMap<String, String> getTablesMap(); /** * Get the FieldHandler for a specific field @@ -57,8 +59,8 @@ public FieldHandler getFieldHandler( DatabaseMap dbMap, String name ) throws ImportHandlerException { - HashMap fMap = getFieldsMap(); - HashMap tMap = getTablesMap(); + HashMap<String, FieldHandler> fMap = getFieldsMap(); + HashMap<String, String> tMap = getTablesMap(); Object res = fMap.get( name ); if ( res == null ) { @@ -66,7 +68,7 @@ String fcomb = ImportParser.parseFinderTable( name ); String fname = ImportParser.extractTable( fcomb ); String ffind = ImportParser.extractFinder( fcomb ); - String table = (String) tMap.get( fname ); + String table = tMap.get( fname ); if ( table == null ) { throw new ImportHandlerException( "Field " + name + " not found" ); @@ -86,7 +88,7 @@ th = dbMap.getFinder( table + '/' + ffind ); def = null; } - final FieldHandler fh = (FieldHandler) fMap.get( fname + "//" ); + final FieldHandler fh = fMap.get( fname + "//" ); if ( fh != null ) { final ImportHandler ih = new ImportHandler( th, def ); Added: trunk/equanda-server/src/main/java/org/equanda/ymport/FieldParser.java =================================================================== --- trunk/equanda-server/src/main/java/org/equanda/ymport/FieldParser.java (rev 0) +++ trunk/equanda-server/src/main/java/org/equanda/ymport/FieldParser.java 2007-09-20 12:24:52 UTC (rev 90) @@ -0,0 +1,101 @@ +/** + * This file is part of the equanda project. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + */ + +package org.equanda.ymport; + +/** + * ... + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public interface FieldParser +{ + public static final FieldParser parseString = new FieldParser() + { + public Object parse( Object obj ) + throws ImportHandlerException + { + return ImportParser.parseString( obj ); + } + }; + public static final FieldParser parseBoolean = new FieldParser() + { + public Object parse( Object obj ) + throws ImportHandlerException + { + return ImportParser.parseBoolean( obj ); + } + }; + public static final FieldParser parseDate = new FieldParser() + { + public Object parse( Object obj ) + throws ImportHandlerException + { + return ImportParser.parseDate( obj ); + } + }; + public static final FieldParser parseTimestamp = new FieldParser() + { + public Object parse( Object obj ) + throws ImportHandlerException + { + return ImportParser.parseTimestamp( obj ); + } + }; + public static final FieldParser parseBlob = new FieldParser() + { + public Object parse( Object obj ) + throws ImportHandlerException + { + return ImportParser.parseBlob( obj ); + } + }; + public static final FieldParser parseClob = new FieldParser() + { + public Object parse( Object obj ) + throws ImportHandlerException + { + return ImportParser.parseClob( obj ); + } + }; + public static final FieldParser parseInt = new FieldParser() + { + public Object parse( Object obj ) + throws ImportHandlerException + { + return ImportParser.parseInt( obj ); + } + }; + public static final FieldParser parseDouble = new FieldParser() + { + public Object parse( Object obj ) + throws ImportHandlerException + { + return ImportParser.parseDouble( obj ); + } + }; + + public Object parse( Object obj ) + throws ImportHandlerException; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |