in blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/acting/OraAddAction.java [65:243]
public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters param) throws Exception {
DataSourceComponent datasource = null;
Connection conn = null;
int currentIndex = 0;
try {
Configuration conf = this.getConfiguration(param.getParameter("descriptor", null));
String query = this.getAddQuery(conf);
datasource = this.getDataSource(conf);
conn = datasource.getConnection();
Request request = ObjectModelHelper.getRequest(objectModel);
if (conn.getAutoCommit()) {
conn.setAutoCommit(false);
}
PreparedStatement statement = conn.prepareStatement(query);
getLogger().info(query);
Configuration[] keys = conf.getChild("table").getChild("keys").getChildren("key");
Configuration[] values = conf.getChild("table").getChild("values").getChildren("value");
currentIndex = 1;
// Insert the keys into the query
for (int i = 0; i < keys.length; i++) {
String mode = keys[i].getAttribute("mode", "automatic");
if ("manual".equals(mode)) {
String selectQuery = this.getSelectQuery(keys[i]);
final Statement stmnt = conn.createStatement();
final ResultSet set = stmnt.executeQuery(selectQuery);
try {
set.next();
int value = set.getInt("maxid") + 1;
statement.setInt(currentIndex, value);
request.setAttribute(keys[i].getAttribute("param"), String.valueOf(value));
set.getStatement().close();
set.close();
currentIndex++;
} finally {
stmnt.close();
}
} else if ("form".equals(mode)) {
String parameter = keys[i].getAttribute("param");
request.setAttribute(parameter, request.getParameter(parameter));
this.setColumn(statement, currentIndex, request, keys[i]);
currentIndex++;
}
}
// insert the values into the query
for (int i = 0; i < values.length; i++) {
String type = values[i].getAttribute("type");
String parameter = values[i].getAttribute("param");
if (type.equals("image")) {
File binaryFile = (File) request.get(parameter);
Parameters iparam = new Parameters();
iparam.setParameter("image-size", String.valueOf(binaryFile.length()));
ImageProperties prop = ImageUtils.getImageProperties(binaryFile);
iparam.setParameter("image-width", Integer.toString(prop.width));
iparam.setParameter("image-height", Integer.toString(prop.height));
synchronized (this.files) {
this.files.put(binaryFile, param);
}
}
if (! this.isLargeObject(type)) {
this.setColumn(statement, currentIndex, request, values[i]);
currentIndex++;
}
}
statement.execute();
statement.close();
query = this.getSelectLOBQuery(conf);
// Process the large objects if they exist
if (query != null) {
PreparedStatement LOBstatement = conn.prepareStatement(query);
getLogger().info(query);
if (keys.length > 0) {
currentIndex = 1;
for (int i = 0; i < keys.length; i++) {
this.setColumn(LOBstatement, currentIndex, request, keys[i]);
currentIndex++;
}
}
OracleResultSet set = (OracleResultSet) LOBstatement.executeQuery();
if (set.next()) {
int index = 0;
for (int i = 0; i < values.length; i++) {
String type = values[i].getAttribute("type", "");
if (this.isLargeObject(type)) {
Object attr = request.get(values[i].getAttribute("param"));
int length = -1;
InputStream stream = null;
OutputStream output = null;
int bufSize = 1024;
index++;
if (type.equals("ascii")) {
CLOB ascii = set.getCLOB(index);
if (attr instanceof File) {
File asciiFile = (File) attr;
stream = new BufferedInputStream(new FileInputStream(asciiFile));
} else {
String asciiText = (String) attr;
stream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes()));
}
output = new BufferedOutputStream(ascii.getAsciiOutputStream());
bufSize = ascii.getBufferSize();
} else {
BLOB binary = set.getBLOB(index);
File binaryFile = (File) attr;
stream = new BufferedInputStream(new FileInputStream(binaryFile));
length = (int) binaryFile.length();
output = new BufferedOutputStream(binary.getBinaryOutputStream());
bufSize = binary.getBufferSize();
}
byte[] buffer = new byte[bufSize];
while ((length = stream.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
stream.close();
output.close();
}
}
}
set.close();
set.getStatement().close();
}
conn.commit();
} catch (Exception e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException se) {
getLogger().debug("There was an error rolling back the transaction", se);
}
}
throw new ProcessingException("Could not add record :position = " + (currentIndex - 1), e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException sqe) {
getLogger().warn("There was an error closing the datasource", sqe);
}
}
if (datasource != null) this.dbselector.release(datasource);
}
return null;
}