/*
* Copyright (C) 2010 Dedicon <http://dedicon.nl>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Linking altText statically or dynamically with other modules
is making a combined work based on altText. Thus, the terms and
conditions of the GNU General Public License cover
the whole combination. In addition, as a special exception,
the copyright holders of altText give you permission to
combine altText program with free software programs or libraries
that are released under the GNU LGPL or the Common Public License version 1.0.
You may copy and distribute such a system following the terms
of the GNU GPL for altText and the licenses of the other code
concerned, provided that you include the source code of that other
code when and as the GNU GPL requires distribution of source code.
Note that people who make modified versions of altText are not obligated to
grant this special exception for their modified versions; it is their
choice whether to do so. The GNU General Public License gives permission
to release a modified version without this exception; this exception
also makes it possible to release a modified version which carries
forward this exception.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author : Javier Asensio Cubero capitan{.}cambio{@}gmail{.}com
*/
package nl.dedicon.converter.odt.processors;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Vector;
import java.util.logging.Logger;
import nl.dedicon.converter.core.Processor;
import nl.dedicon.converter.core.io.Package;
import nl.dedicon.converter.core.util.ConfigurationManager;
import nl.dedicon.converter.odt.io.OdtPackage;
import nl.dedicon.converter.service.ServiceException;
import nl.dedicon.converter.service.db.DbFile;
import nl.dedicon.converter.service.db.DbHelper;
// TODO: Auto-generated Javadoc
/**
* The Class MetadataStorerProcessor stores the metadata elements in db in case the converter is being run as a service.
*/
public class MetadataStorerProcessor extends Processor {
/** The logger. */
private Logger logger = Logger
.getLogger("nl.dedicon.converter.odt.processors");
/** The Constant RUNNING_AS_SERVICE. */
public static final String RUNNING_AS_SERVICE = "iAmService";
/* (non-Javadoc)
* @see nl.dedicon.converter.core.Processor#doPerform(nl.dedicon.converter.core.io.Package)
*/
@Override
protected void doPerform(Package pack) {
this.logger.entering("MetadataStorerProcessor", "doPerform");
logger.info("Performing " + this.getClass().getName());
OdtPackage oPack = (OdtPackage) pack;
if (!ConfigurationManager.getInstance().getConfItem(RUNNING_AS_SERVICE)
.isEmpty()) {
this.storeMetadata(oPack);
}
this.logger.exiting("MetadataStorerProcessor", "doPerform");
}
/**
* Stores metadata.
*
* @param oPack the o pack
*/
private void storeMetadata(OdtPackage oPack) {
try {
DbFile file = (DbFile) oPack.getOriginalFile();
MetaDataDbHelper mDbH = new MetaDataDbHelper();
mDbH.storeMetadataElements(oPack.getMetadata(), file.getId());
} catch (Exception e) {
logger.warning("Error adding metadata to the database:"
+ e.getMessage());
}
}
/**
* The Class MetaDataDbHelper has the connection and sql statements to store the metadata elements.
*/
static class MetaDataDbHelper extends DbHelper {
/** The m con. */
Connection mCon;
/**
* Instantiates a new meta data db helper.
*
* @throws ServiceException the service exception
*/
public MetaDataDbHelper() throws ServiceException {
super();
this.open();
mCon = this.getConnection();
}
/**
* Store metadata elements.
*
* @param values the values
* @param fileId the file id
*
* @throws SQLException the SQL exception
*/
public void storeMetadataElements(HashMap<String, String> values,
int fileId) throws SQLException {
String sql = "replace into meta_data (file_id,meta_data_element,value) values ";
String newEntry = " (?,?,?), ";
Vector<String> params = new Vector<String>();
for (String key : values.keySet()) {
params.add(fileId + "");
params.add(key);
params.add(values.get(key));
sql += newEntry;
}
// last comma
sql = sql.substring(0, sql.length() - 2);
PreparedStatement stmt = mCon.prepareStatement(sql);
for (int i = 1; i <= params.size(); i++) {
stmt.setString(i, params.elementAt(i-1));
}
stmt.execute();
}
}
}
|