📄 cmsdbaccess.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/file/mySql/Attic/CmsDbAccess.java,v $
* Date : $Date: 2003/03/18 17:48:23 $
* Version: $Revision: 1.86 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.file.mySql;
import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.file.*;
import com.opencms.util.Encoder;
import com.opencms.util.SqlHelper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import source.org.apache.java.util.Configurations;
/**
* This is the generic access module to load and store resources from and into
* the database.
*
* @author Andreas Schouten
* @author Michael Emmerich
* @author Hanjo Riege
* @author Anders Fugmann
* @version $Revision: 1.86 $ $Date: 2003/03/18 17:48:23 $ *
*/
public class CmsDbAccess extends com.opencms.file.genericSql.CmsDbAccess implements I_CmsConstants, I_CmsLogChannels {
private static Boolean m_escapeStrings = null;
/**
* Returns <code>true</code> if Strings must be escaped before they are stored in the DB,
* this is required because MySQL does not support multi byte unicode strings.<p>
*
* @return boolean <code>true</code> if Strings must be escaped before they are stored in the DB
*/
private boolean singleByteEncoding() {
if (m_escapeStrings == null) {
String encoding = A_OpenCms.getDefaultEncoding();
m_escapeStrings = new Boolean (
"ISO-8859-1".equalsIgnoreCase(encoding) ||
"ISO-8859-15".equalsIgnoreCase(encoding) ||
"US-ASCII".equalsIgnoreCase(encoding) ||
"Cp1252".equalsIgnoreCase(encoding)
);
}
return m_escapeStrings.booleanValue();
}
/**
* Escapes a String to prevent issues with UTF-8 encoding, same style as
* http uses for form data since MySQL doesn't support Unicode/UTF-8 strings.<p>
*
* @param value String to be escaped
* @return the escaped String
*/
private String escape(String value) {
// no need to encode if OpenCms is not running in Unicode mode
if (singleByteEncoding()) return value;
return Encoder.encode(value);
}
/**
* Unescapes a String to prevent issues with UTF-8 encoding, same style as
* http uses for form data since MySQL doesn't support Unicode/UTF-8 strings.<p>
*
* @param value String to be unescaped
* @return the unescaped String
*/
private String unescape(String value) {
// no need to encode if OpenCms is not running in Unicode mode
if (singleByteEncoding()) return value;
return Encoder.decode(value);
}
/**
* Instanciates the access-module and sets up all required modules and connections.
* @param config The OpenCms configuration.
* @throws CmsException Throws CmsException if something goes wrong.
*/
public CmsDbAccess(Configurations config)
throws CmsException {
super(config);
}
/**
* Adds a user to the database.
*
* @param name username
* @param password user-password
* @param description user-description
* @param firstname user-firstname
* @param lastname user-lastname
* @param email user-email
* @param lastlogin user-lastlogin
* @param lastused user-lastused
* @param flags user-flags
* @param additionalInfos user-additional-infos
* @param defaultGroup user-defaultGroup
* @param address user-defauladdress
* @param section user-section
* @param type user-type
*
* @return the created user.
* @throws thorws CmsException if something goes wrong.
*/
public CmsUser addUser(String name, String password, String description,
String firstname, String lastname, String email,
long lastlogin, long lastused, int flags, Hashtable additionalInfos,
CmsGroup defaultGroup, String address, String section, int type)
throws CmsException {
int id = nextId(C_TABLE_USERS);
byte[] value=null;
PreparedStatement statement = null;
Connection con = null;
try {
con = DriverManager.getConnection(m_poolName);
// serialize the hashtable
ByteArrayOutputStream bout= new ByteArrayOutputStream();
ObjectOutputStream oout=new ObjectOutputStream(bout);
oout.writeObject(additionalInfos);
oout.close();
value=bout.toByteArray();
// write data to database
statement = con.prepareStatement(m_cq.get("C_USERS_ADD"));
statement.setInt(1,id);
statement.setString(2,name);
// crypt the password with MD5
statement.setString(3, digest(password));
statement.setString(4, digest(""));
statement.setString(5,description);
statement.setString(6,firstname);
statement.setString(7,lastname);
statement.setString(8,email);
statement.setTimestamp(9, new Timestamp(lastlogin));
statement.setTimestamp(10, new Timestamp(lastused));
statement.setInt(11,flags);
statement.setBytes(12,value);
statement.setInt(13,defaultGroup.getId());
statement.setString(14,address);
statement.setString(15,section);
statement.setInt(16,type);
statement.executeUpdate();
}
catch (SQLException e){
throw new CmsException("["+this.getClass().getName()+"]"+e.getMessage(),CmsException.C_SQL_ERROR, e);
}
catch (IOException e){
throw new CmsException("[CmsAccessUserInfoMySql/addUserInformation(id,object)]:"+CmsException. C_SERIALIZATION, e);
} finally {
// close all db-resources
if(statement != null) {
try {
statement.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(con != null) {
try {
con.close();
} catch(SQLException exc) {
// nothing to do here
}
}
}
return readUser(id);
}
/**
* Deletes all files in CMS_FILES without fileHeader in CMS_RESOURCES
*
*
*/
protected void clearFilesTable()
throws CmsException{
PreparedStatement statementSearch = null;
PreparedStatement statementDestroy = null;
ResultSet res = null;
Connection con = null;
try{
con = DriverManager.getConnection(m_poolName);
statementSearch = con.prepareStatement(m_cq.get("C_RESOURCES_GET_LOST_ID"));
res = statementSearch.executeQuery();
// delete the lost fileId's
statementDestroy = con.prepareStatement(m_cq.get("C_FILE_DELETE"));
while (res.next() ){
statementDestroy.setInt(1,res.getInt(m_cq.get("C_FILE_ID")));
statementDestroy.executeUpdate();
statementDestroy.clearParameters();
}
} catch (SQLException e){
throw new CmsException("["+this.getClass().getName()+"] "+e.getMessage(),CmsException.C_SQL_ERROR, e);
}finally {
// close all db-resources
if(res != null) {
try {
res.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(statementDestroy != null) {
try {
statementDestroy.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(statementSearch != null) {
try {
statementSearch.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(con != null) {
try {
con.close();
} catch(SQLException exc) {
// nothing to do here
}
}
}
}
/**
* Creates a new file with the given content and resourcetype.
*
* @param user The user who wants to create the file.
* @param project The project in which the resource will be used.
* @param onlineProject The online project of the OpenCms.
* @param filename The complete name of the new file (including pathinformation).
* @param flags The flags of this resource.
* @param parentId The parentId of the resource.
* @param contents The contents of the new file.
* @param resourceType The resourceType of the new file.
*
* @return file The created file.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -