oracle.otnsamples.cmsxdb.dbmanager.CMSXDBServletUtils (Java2HTML)
package oracle.otnsamples.cmsxdb.dbmanager;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Hashtable;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.io.IOException;
import java.io.BufferedReader;
import oracle.sql.CLOB;
import oracle.otnsamples.cmsxdb.exception.CMSAccessException;
public class CMSXDBServletUtils {
public static Collection getFolderContents( String abspath,
String username,
String type,
Connection connection )
throws CMSAccessException {
String folderqry =
" SELECT DISTINCT extractValue(link, '/LINK/Name/text()') "+
" FROM XDB.path_view "+
" WHERE UNDER_PATH(res, 1, ?) = 1 "+
" AND extractValue(res, '/Resource/@Container') = 'true' ";
String fileqry =
" SELECT DISTINCT extractValue(link, '/LINK/Name/text()') "+
" FROM XDB.path_view "+
" WHERE UNDER_PATH(res, 1, ?) = 1 "+
" AND extractValue(res, '/Resource/@Container') = 'false' ";
String folderdtls =
" SELECT DISTINCT TO_CHAR(extractvalue(res, '/Resource/Owner/text()')) "+
" ,SUBSTR(TO_CHAR(extractvalue(res, '/Resource/CreationDate/text()'), 'DD-MON-YYYY'), 1, 12) "+
" ,SUBSTR(TO_CHAR(extractvalue(res, '/Resource/ModificationDate/text()'), 'DD-MON-YYYY'), 1, 12) "+
" ,TO_CHAR(extractvalue(res, '/Resource/Language/text()')) "+
" FROM XDB.resource_view "+
" WHERE any_path LIKE (? || '/' || ?) ";
String filedtls =
" SELECT DISTINCT TO_CHAR(extractvalue(res, '/Resource/Owner/text()')) "+
" ,SUBSTR(TO_CHAR(extractvalue(res, '/Resource/CreationDate/text()'), 'DD-MON-YYYY'), 1, 12) "+
" ,SUBSTR(TO_CHAR(extractvalue(res, '/Resource/ModificationDate/text()'), 'DD-MON-YYYY'), 1, 12) "+
" ,TO_CHAR(extractvalue(res, '/Resource/Language/text()')) "+
" ,TO_CHAR(extractvalue(res, '/Resource/ContentType/text()')) "+
" ,TO_CHAR(NVL(extractvalue(res, '/Resource@VersionID'), '-1') ) "+
" ,TO_CHAR(NVL(DBMS_LOB.GETLENGTH(XDBUriType(any_path).getclob()), 0) ) "+
" FROM XDB.resource_view "+
" WHERE any_path LIKE (? || '/' || ?) ";
PreparedStatement pstmt = null;
ResultSet rset = null;
PreparedStatement pstmtdtls = null;
ResultSet rsetdtls = null;
Collection retcoll = new ArrayList();
try {
if ("FOLDER".equals(type)) {
pstmt = connection.prepareStatement(folderqry);
pstmtdtls = connection.prepareStatement(folderdtls);
} else if ("FILE".equals(type)) {
pstmt = connection.prepareStatement(fileqry);
pstmtdtls = connection.prepareStatement(filedtls);
}
pstmt.setString(1, abspath);
pstmtdtls.setString(1, abspath);
rset = pstmt.executeQuery();
while (rset.next()) {
String name = rset.getString(1);
Hashtable hash = new Hashtable();
hash.put("DisplayName", name);
try {
pstmtdtls.setString(2, name);
rsetdtls = pstmtdtls.executeQuery();
if (rsetdtls.next()) {
hash.put("Owner", rsetdtls.getString(1));
hash.put("CreationDate", rsetdtls.getString(2));
hash.put("ModificationDate", rsetdtls.getString(3));
hash.put("Language", rsetdtls.getString(4));
if ("FILE".equals(type)) {
String contentType = rsetdtls.getString(5);
if (name.toLowerCase().endsWith(".xml")) {
contentType = getPersContentType(abspath+"/"+name,
username, connection);
}
hash.put("ContentType", contentType);
hash.put("VersionId", rsetdtls.getString(6));
String size = rsetdtls.getString(7);
int intsize = (int)Math.ceil(Double.parseDouble(size) / 1000);
hash.put("Size", Integer.toString(intsize));
}
}
retcoll.add(hash);
} catch(SQLException ex) {
throw new CMSAccessException ( "SQL Exception in getFolderContents " +
"for type : " + type + " " +
"while getting details : " +
ex.toString() );
} finally {
if (rsetdtls != null) rsetdtls.close();
}
}
} catch(SQLException ex) {
throw new CMSAccessException ( "SQL Exception in getFolderContents " +
"for type : " + type + " " +
"while getting the list : " +
ex.toString() );
} finally {
try {
if ( rset != null ) rset.close(); if ( pstmt != null ) pstmt.close(); if ( pstmtdtls != null ) pstmtdtls.close();
} catch(SQLException ex) {
throw new CMSAccessException ( "SQL Exception in getFolderContents " +
"while closing statements : " +
ex.toString() );
}
}
return retcoll;
}
private static boolean checkView(String resource, String username,
String xslloc, String viewtype,
Connection connection)
throws CMSAccessException {
PreparedStatement pstmt = null;
ResultSet rset = null;
String query = " SELECT viewtype, op.xslloc.getexternalurl() "+
" FROM CMSADMIN.otncms_pers_view op "+
" WHERE UPPER(op.username) = UPPER(?) "+
" AND op.resourceloc.geturl() = ? ";
try {
pstmt = connection.prepareStatement(query);
pstmt.setString(1, username);
pstmt.setString(2, resource);
rset = pstmt.executeQuery();
if (rset.next()) { return true;
}
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in checkView " +
ex.toString() );
} finally {
try {
if ( rset != null ) rset.close(); if ( pstmt != null ) pstmt.close();
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in checkView " +
"while closing statements : "+
ex.toString() );
}
}
return false;
}
private static boolean checkXSL(String xslloc, Connection connection )
throws CMSAccessException {
PreparedStatement pstmt = null;
ResultSet rset = null;
String query = " SELECT any_path "+
" FROM XDB.resource_view "+
" WHERE any_path = ? ";
try {
pstmt = connection.prepareStatement(query);
pstmt.setString(1, xslloc);
rset = pstmt.executeQuery();
if (rset.next()) {
return true;
}
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in checkXSL " +
ex.toString() );
} finally {
try {
if ( rset != null ) rset.close(); if ( pstmt != null ) pstmt.close();
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in checkXSL " +
"while closing statements : "+
ex.toString() );
}
}
return false;
}
public static String insertView(String resource, String username,
String xslloc, String viewtype,
Connection connection)
throws CMSAccessException {
String retMessage = "<EXC>Could not insert into CMSADMIN.OTNCMS_PERS_VIEW</EXC>";
PreparedStatement pstmt = null;
String query =
" INSERT INTO CMSADMIN.otncms_pers_view "+
" ( USERNAME, RESOURCELOC, VIEWTYPE, XSLLOC ) "+
" VALUES ( UPPER(?), SYS.XDBURITYPE(?), ?, SYS.XDBURITYPE(?) ) ";
try {
boolean flag = checkView(resource, username, xslloc,
viewtype, connection);
if (flag) {
retMessage = "<EXC>Record already exists</EXC>";
} else {
boolean resxsl = true;
if (!("".equals(xslloc))) {
resxsl = checkXSL(xslloc, connection);
}
if (resxsl) {
pstmt = connection.prepareStatement(query);
pstmt.setString(1, username);
pstmt.setString(2, resource);
pstmt.setString(3, viewtype);
pstmt.setString(4, xslloc);
if (pstmt.executeUpdate() == 1) {
retMessage = "<MSG>Record created successfully</MSG>";
connection.commit();
}
} else {
retMessage = "<EXC>The XSL is not a resource</EXC>";
}
}
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in insertView " +
ex.toString() );
} finally {
try {
if ( pstmt != null ) pstmt.close();
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in insertView " +
"while closing statements : "+
ex.toString() );
}
}
return retMessage;
}
public static String updateView(String resource, String username,
String xslloc, String viewtype,
Connection connection)
throws CMSAccessException {
String retMessage = "<EXC>Could not update CMSADMIN.OTNCMS_PERS_VIEW</EXC>";
PreparedStatement pstmt = null;
String query = " UPDATE CMSADMIN.otncms_pers_view op "+
" SET op.viewtype = ? "+
" , op.xslloc = SYS.XDBURITYPE(?) "+
" WHERE UPPER(op.username) = UPPER(?) "+
" AND op.resourceloc.geturl() = ? ";
try {
boolean flag = checkView(resource, username, xslloc,
viewtype, connection);
if (flag) {
boolean resxsl = true;
if (!("".equals(xslloc))) {
resxsl = checkXSL(xslloc, connection);
}
if (resxsl) {
pstmt = connection.prepareStatement(query);
pstmt.setString(1, viewtype);
pstmt.setString(2, xslloc);
pstmt.setString(3, username);
pstmt.setString(4, resource);
if (pstmt.executeUpdate() == 1) {
retMessage = "<MSG>Record updated successfully</MSG>";
connection.commit();
}
} else {
retMessage = "<EXC>The XSL is not a resource</EXC>";
}
} else {
retMessage = "<EXC>Record does not exist</EXC>";
}
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in updateView " +
ex.toString() );
} finally {
try {
if (pstmt != null) pstmt.close();
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in updateView " +
"while closing statements : "+
ex.toString() );
}
}
return retMessage;
}
private static String getStringFromClob(CLOB clob)
throws CMSAccessException {
StringBuffer sb = new StringBuffer();
BufferedReader clobData = null;
try {
clobData = new BufferedReader(clob.getCharacterStream());
String s;
while( ( s = clobData.readLine( ) ) != null ) {
sb.append( s + "\n");
}
} catch (SQLException sqlex) {
throw new CMSAccessException ( "SQLException in getStringFromClob " +
sqlex.toString() );
} catch (IOException ioex) {
throw new CMSAccessException ( "IOException in getStringFromClob " +
ioex.toString() );
} finally {
try {
if ( clobData != null ) clobData.close();
} catch (IOException ioex) {
throw new CMSAccessException ( "IOException in getStringFromClob " +
"while closing the CLOB data " +
ioex.toString() );
}
}
return sb.toString();
}
public static String getPrevContents(String abspath, String version,
String action, Connection connection)
throws CMSAccessException {
CallableStatement cstmt = null;
String status = "Failed to get contents of the previous version";
String query = " begin ? := CMSADMIN.otncms_xmlcontent.getprevcontents( "+
" ?, ?, ?).getclobval(); end; ";
try {
cstmt = connection.prepareCall( query );
cstmt.registerOutParameter(1, Types.CLOB);
cstmt.setString(2, abspath); cstmt.setString(3, version); cstmt.setString(4, action);
cstmt.execute();
status = getStringFromClob((oracle.sql.CLOB)cstmt.getClob(1));
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in getPrevContents " +
ex.toString() );
} finally {
try {
if (cstmt != null) cstmt.close();
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in getPrevContents " +
"while closing statements : "+
ex.toString() );
}
}
return status;
}
private static String getPersContentType(String resource, String username,
Connection connection)
throws CMSAccessException {
String contentType = "text/xml";
PreparedStatement pstmt = null;
ResultSet rset = null;
String query =
" SELECT viewtype "+
" FROM CMSADMIN.otncms_pers_view op "+
" WHERE op.username = ? "+
" AND op.resourceloc.geturl() = ? ";
try {
pstmt = connection.prepareStatement(query);
pstmt.setString(1, username);
pstmt.setString(2, resource);
rset = pstmt.executeQuery();
if (rset.next()) {
contentType = rset.getString(1);
}
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in getPersContentType " +
ex.toString() );
} finally {
try {
if ( rset != null ) rset.close(); if ( pstmt != null ) pstmt.close();
} catch(SQLException ex) {
throw new CMSAccessException ( "SQLException in getPersContentType " +
"while closing statements : "+
ex.toString() );
}
}
return contentType;
}
}
|