bloritsch 2002/10/24 14:47:55
Added: infomover/src/java/org/apache/infomover/manipulator
Normalizer.java
Log:
Adding a Normalizer
Revision Changes Path
1.1
jakarta-avalon-apps/infomover/src/java/org/apache/infomover/manipulator/Normalizer.java
Index: Normalizer.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.infomover.manipulator;
import org.apache.infomover.transaction.Transaction;
import org.apache.infomover.transaction.Record;
import org.apache.infomover.transaction.Field;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import java.util.*;
/**
* Normalizer does ....
*.
* @author <a href="[EMAIL PROTECTED]>Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/10/24 21:47:55 $
*/
public class Normalizer
extends AbstractLogEnabled
implements Manipulator, Configurable
{
private final static String KEYSET = Normalizer.class.getName();
private String m_tableName;
private String m_destTable;
/** Sets up the field name to key entry map */
private Map m_fieldNames;
private KeyEntry m_multiplexField;
public Normalizer()
{
m_tableName = "";
m_fieldNames = new HashMap();
m_destTable = "";
m_fieldNames.put( KEYSET, new HashSet() );
}
public void configure( Configuration config )
throws ConfigurationException
{
Configuration tableEntries = config.getChild( "table-entries" );
m_tableName = tableEntries.getAttribute( "source" );
m_destTable = tableEntries.getAttribute( "dest" );
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "The source table is \"" + m_tableName +
"\", and the destination table is \"" +
m_destTable + "\".");
}
Configuration[] fields = tableEntries.getChildren( "field" );
for ( int i = 0; i < fields.length; i++ )
{
String fieldName = fields[i].getAttribute( "field" );
String entry = fields[i].getAttribute( "entry" );
m_fieldNames.put( fieldName, entry );
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( fieldName + " = " + entry );
}
}
Set keySet = (Set) m_fieldNames.get( KEYSET );
Configuration[] keys = tableEntries.getChildren( "key" );
boolean hasMultiplexEntry = false;
for ( int i = 0; i < keys.length; i++ )
{
KeyEntry entry = new KeyEntry( keys[i].getAttribute( "name" ),
keys[i].getAttributeAsBoolean( "multiplex" ,
false ) );
keySet.add( entry );
if ( entry.multiplexEntry )
{
if ( hasMultiplexEntry )
{
throw new ConfigurationException( "We can only have one
multiplex key" );
}
else
{
hasMultiplexEntry = true;
m_multiplexField = entry;
}
}
}
}
public Transaction process( Transaction trans )
{
Set recordSet = trans.getRecordset( m_tableName );
Iterator it = recordSet.iterator();
while ( it.hasNext() )
{
processRecord( trans, (Record) it.next() );
trans.removeRecord( rec );
}
return trans;
}
private void processRecord( Transaction trans, Record rec )
{
Iterator it = m_fieldNames.keySet().iterator();
while ( it.hasNext() )
{
String fieldName = (String) it.next();
if ( KEYSET.equals( fieldName ) ) continue;
Field entryField = new Field( m_multiplexField.name,
m_fieldNames.get( fieldName ), Field.STRING );
List fields = createFields( rec, entryField );
boolean add = rec.getField( fieldName ).getValue().equals(
Boolean.TRUE );
Record rec = new Record( m_destTable, (add) ? Record.ADD :
Record.DELETE );
trans.addRecord( rec );
}
}
private List createFields( Record rec, Field multiplexField )
{
List fields = new ArrayList();
Iterator it = ( (Set)m_fieldNames.get( KEYSET ) ).iterator();
while ( it.hasNext() )
{
KeyEntry entry = (KeyEntry)it.next();
Field field = null;
if ( entry.multiplexEntry )
{
field = multiplexField;
}
else
{
field = rec.getField( entry.name );
}
if ( null == field && getLogger().isErrorEnabled() )
{
getLogger().error( "A key field was missing!" + entry.name );
}
fields.add( field );
}
return fields;
}
private static final class KeyEntry
{
public final String name;
public final boolean multiplexEntry;
public KeyEntry( String name, boolean multiplex )
{
this.name = name;
multiplexEntry = multiplex;
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>