crudhelper.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 558 行
JAVA
558 行
package Jt.wizard.struts;
import java.io.File;
import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import Jt.JtContext;
import Jt.JtFactory;
import Jt.JtHashTable;
import Jt.JtIterator;
import Jt.JtMessage;
import Jt.JtObject;
import Jt.DAO.JtDAOAdapter;
import Jt.DAO.JtDAOFileAdapter;
import Jt.hibernate.JtHibernateAdapter;
import Jt.wizard.WizardConfig;
import org.apache.struts.action.ActionForm;
/**
* Implements CRUD (Create, Read, Update and Delete).
*/
public class CRUDHelper extends JtObject {
private static final long serialVersionUID = 1L;
/*
public static final String READ = "READ";
public static final String DELETE = "DELETE";
public static final String UPDATE = "UPDATE";
public static final String CREATE = "CREATE";
public static final String EXECUTE_QUERY = "EXECUTE_QUERY";
*/
public static final String JtCLASS_NAME = CRUDHelper.class.getName();
private JtFactory factory = new JtFactory ();
private transient JtDAOFileAdapter adapter;
private String key;
private String classname;
private Object classInstance;
private String query;
private WizardConfig config;
private String filename;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
private void handleUIError (String error) {
Collection col;
if (error == null)
return;
col = (Collection) this.getObjErrors();
if (col == null) {
col = new LinkedList ();
setObjErrors (col);
}
col.add(error);
}
private String concatenatePath (String path, String subpath) {
char separator = File.separatorChar;
File file;
if (path == null)
return null;
if (subpath == null)
return path;
file = new File (path);
return (file.getAbsolutePath() + separator + subpath);
}
private Object readObject (ActionForm form, String content) {
JtMessage msg = new JtMessage ();
Object obj;
ActionForm oform;
String path;
if (form == null)
return (null); // check
if (classname == null) {
handleError ("classname attribute needs to set.");
return (null);
}
if (classInstance == null) {
classInstance = factory.createObject (classname);
if (classInstance == null) {
handleError ("unable to create an object of class: " + classname);
return (null);
}
}
config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
if (config == null) {
handleError ("Unable to create configuration class");
return (null);
}
factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));
if (filename == null) {
handleError ("Attribute filename needs to be set.");
return (null);
}
adapter = (JtDAOFileAdapter) factory.createObject (JtDAOFileAdapter.JtCLASS_NAME);
path = config.getWorkingDirectory();
adapter.setPath(concatenatePath (path, filename));
msg.setMsgId (JtDAOAdapter.JtREAD);
//msg.setMsgContent(classInstance);
obj = factory.sendMessage (adapter, msg);
// Propagate the exception
if (adapter.getObjException() != null) {
this.setObjException(adapter.getObjException());
//factory.sendMessage (adapter, new JtMessage (JtObject.JtREMOVE));
return (null);
}
if (obj == null)
handleUIError ("Unable to find item.");
factory.sendMessage (adapter, new JtMessage (JtObject.JtREMOVE));
if (obj == null)
return (null);
oform = (ActionForm) factory.createObject(form.getClass().getName());
copyToForm (obj, oform);
return (oform);
}
private JtHashTable getAttributes (Object obj) {
JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
if (obj == null)
return (null);
msg.setMsgContent(obj);
return ((JtHashTable) factory.processMessage(msg));
}
private void copyFormAttributes (ActionForm form, Object obj) {
JtHashTable attributes;
JtIterator iterator;
String attribute;
Object value;
if (obj == null || form == null)
return;
attributes = getAttributes (obj);
if (attributes == null)
return;
iterator = (JtIterator) attributes.processMessage(new JtMessage (JtHashTable.JtGET_KEYS));
if (iterator == null)
return;
for (;;) {
attribute = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));
if (attribute == null)
break;
value = factory.getValue(form,attribute);
factory.setValue (obj, attribute, value);
}
}
private String DateToString (Date date) {
String sdate;
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
if (date == null)
return null;
try {
sdate = df.format (date);
} catch (Exception e) {
handleException (e);
return null;
}
return (sdate);
}
private void copyToForm (Object obj, ActionForm form) {
JtHashTable attributes;
JtIterator iterator;
String attribute;
Object value;
if (obj == null || form == null)
return;
attributes = getAttributes (obj);
if (attributes == null)
return;
iterator = (JtIterator) attributes.processMessage(new JtMessage (JtHashTable.JtGET_KEYS));
if (iterator == null)
return;
for (;;) {
attribute = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));
if (attribute == null)
break;
value = factory.getValue(obj,attribute);
if (value instanceof Date)
factory.setValue (form, attribute, DateToString ((Date) value));
else
factory.setValue (form, attribute, value);
}
}
private void deleteObject (ActionForm form) {
JtMessage msg = new JtMessage ();
//JtHashTable attributes;
//Member member = new Member ();
if (form == null)
return; // check
adapter = (JtDAOFileAdapter) factory.createObject (JtHibernateAdapter.JtCLASS_NAME);
classInstance = factory.createObject (classname);
if (classInstance == null) {
handleError ("unable to create an object of class: " + classname);
return;
}
copyFormAttributes (form, classInstance);
msg = new JtMessage (JtDAOAdapter.JtDELETE);
msg.setMsgContent(classInstance);
factory.sendMessage (adapter, msg);
// Propagate the exception
if (adapter.getObjException() != null) {
this.setObjException(adapter.getObjException());
}
factory.sendMessage (adapter, new JtMessage (JtObject.JtREMOVE));
}
private void updateObject (ActionForm form) {
JtMessage msg = new JtMessage ();
String path;
//Member member = new Member ();
if (form == null)
return; // check
//adapter = (JtDAOFileAdapter) factory.createObject (JtHibernateAdapter.JtCLASS_NAME);
adapter = (JtDAOFileAdapter) factory.createObject (JtDAOFileAdapter.JtCLASS_NAME);
config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
if (config == null) {
handleError ("Unable to create configuration class");
return;
}
factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));
if (filename == null) {
handleError ("Attribute filename needs to be set.");
return;
}
path = config.getWorkingDirectory();
adapter.setPath(concatenatePath (path, filename));
classInstance = factory.createObject (classname);
if (classInstance == null) {
handleError ("unable to create an object of class: " + classname);
return;
}
copyFormAttributes (form, classInstance);
msg = new JtMessage (JtDAOAdapter.JtUPDATE);
msg.setMsgContent(classInstance);
factory.sendMessage (adapter, msg);
// Propagate the exception
if (adapter.getObjException() != null) {
this.setObjException(adapter.getObjException());
}
factory.sendMessage (adapter, new JtMessage (JtObject.JtREMOVE));
}
private void createObject (ActionForm form) {
JtMessage msg = new JtMessage ();
//Member member = new Member ();
if (form == null)
return; // check
adapter = (JtDAOFileAdapter) factory.createObject (JtHibernateAdapter.JtCLASS_NAME);
classInstance = factory.createObject (classname);
if (classInstance == null) {
handleError ("unable to create an object of class: " + classname);
return;
}
copyFormAttributes (form, classInstance);
msg = new JtMessage (JtDAOAdapter.JtCREATE);
msg.setMsgContent(classInstance);
factory.sendMessage (adapter, msg);
// Propagate the exception
if (adapter.getObjException() != null) {
this.setObjException(adapter.getObjException());
}
factory.sendMessage (adapter, new JtMessage (JtObject.JtREMOVE));
}
private List executeQuery () {
JtMessage msg = new JtMessage ();
List result;
adapter = (JtDAOFileAdapter) factory.createObject (JtHibernateAdapter.JtCLASS_NAME);
msg.setMsgId (JtHibernateAdapter.JtEXECUTE_QUERY);
if (query == null || query.equals("")) {
handleError ("query attribute needs to be set.");
return (null);
}
//msg.setMsgContent("select * from roster");
msg.setMsgContent(query);
classInstance = factory.createObject (classname);
if (classInstance == null) {
handleError ("unable to create an object of class: " + classname);
return (null);
}
msg.setMsgData(classInstance);
result = (List) factory.sendMessage (adapter, msg);
// Propagate the exception
if (adapter.getObjException() != null) {
this.setObjException(adapter.getObjException());
result = null;
}
factory.sendMessage (adapter, new JtMessage (JtObject.JtREMOVE));
return (result);
}
/**
* Process object messages.
* <ul>
* <li> JtACTIVATE
* </ul>
* @param message message
*/
public Object processMessage (Object message) {
Object data;
String content;
JtContext context;
ActionForm form = null;
JtMessage e = (JtMessage) message;
if (e == null || (e.getMsgId() == null))
return (null);
factory.setStopClass(JtObject.class);
/*
if (e.getMsgId().equals(JtObject.JtACTIVATE)) {
// pass the form information
data = e.getMsgData ();
return ( getActionMappings ());
}
*/
context = (JtContext) e.getMsgContext();
if (context != null)
form = (ActionForm) context.getActionForm();
if (e.getMsgId().equals(JtDAOAdapter.JtREAD)) {
content = (String) e.getMsgContent ();
//data = e.getMsgData ();
return (readObject ((ActionForm) form, content));
}
if (e.getMsgId().equals(JtDAOAdapter.JtDELETE)) {
//data = e.getMsgData ();
deleteObject ((ActionForm) form);
return (null);
}
if (e.getMsgId().equals(JtDAOAdapter.JtUPDATE)) {
//data = e.getMsgData ();
updateObject ((ActionForm) form);
return (null);
}
if (e.getMsgId().equals(JtDAOAdapter.JtCREATE)) {
//data = e.getMsgData ();
createObject ((ActionForm) form);
return (null); // check
}
return (super.processMessage(message));
}
/**
* Demonstrates the messages processed by this class.
*/
/*
public static void main(String[] args) {
JtObject factory = new JtFactory ();
CRUDHelper crud;
JtMessage msg;
//List list;
Object obj;
MemberForm form = new MemberForm ();
msg = new JtMessage (JtDAOAdapter.JtCREATE);
}
*/
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?