Added Object DSO class and HttpRedirect defaults to temporary moved
This commit is contained in:
parent
f34ac6f961
commit
71c8d017ca
4 changed files with 117 additions and 5 deletions
|
|
@ -58,9 +58,9 @@ import java.util.logging.Logger;
|
|||
* *Double
|
||||
* *String
|
||||
* *Character
|
||||
* *DBBean
|
||||
* *java.sql.Timestamp
|
||||
* *List<DBBean>
|
||||
* *java.sql.Timestamp
|
||||
* *DBBean (A Integer reference to another Bean in another table)
|
||||
* *List<DBBean> (A reference table is used to associate Beans into the list)
|
||||
* </XMP>
|
||||
* @author Ziver
|
||||
*/
|
||||
|
|
|
|||
106
src/zutil/db/bean/DBBeanObjectDSO.java
Executable file
106
src/zutil/db/bean/DBBeanObjectDSO.java
Executable file
|
|
@ -0,0 +1,106 @@
|
|||
package zutil.db.bean;
|
||||
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.parser.json.JSONParser;
|
||||
import zutil.parser.json.JSONWriter;
|
||||
import zutil.ui.Configurator;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
/**
|
||||
* A intermediate class for loading Objects of generic Classes.
|
||||
* The extending class must set the "superBean" parameter to true in {@link @DBBean.DBTable}.
|
||||
* The Object that is stored must use Configurator to define what fields that should be stored.
|
||||
*
|
||||
* This class needs to fields in DB:
|
||||
* <ul>
|
||||
* <li>String type: defining the class name</li>
|
||||
* <li>Text config: the object configuration is stored as JSON</li>
|
||||
* </ul>
|
||||
*/
|
||||
public abstract class DBBeanObjectDSO<T> extends DBBean{
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
// DB parameters
|
||||
private String type;
|
||||
/** Used to store the Object configuration in DB */
|
||||
private String config;
|
||||
|
||||
// Local parameters
|
||||
private transient T cachedObj;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void postUpdateAction() {
|
||||
if (type != null && !type.isEmpty()) {
|
||||
if (cachedObj == null) {
|
||||
try {
|
||||
Class clazz = Class.forName(type);
|
||||
cachedObj = (T) clazz.newInstance();
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "Unable instantiate class: " + type, e);
|
||||
}
|
||||
}
|
||||
|
||||
if (config != null && !config.isEmpty()) {
|
||||
Configurator<T> configurator = new Configurator<>(cachedObj);
|
||||
configurator.setValues(JSONParser.read(config));
|
||||
configurator.applyConfiguration();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(DBConnection db) throws SQLException {
|
||||
if (cachedObj == null)
|
||||
this.config = null;
|
||||
else {
|
||||
Configurator<T> configurator = new Configurator<>(cachedObj);
|
||||
this.config = JSONWriter.toString(configurator.getValuesAsNode());
|
||||
}
|
||||
super.save(db);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public T getObject(){
|
||||
return cachedObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will replace the current object.
|
||||
*/
|
||||
public void setObject(T obj){
|
||||
if(obj != null) {
|
||||
type = obj.getClass().getName();
|
||||
config = null;
|
||||
cachedObj = obj;
|
||||
} else {
|
||||
type = null;
|
||||
config = null;
|
||||
cachedObj = null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getObjectClass(){
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setObjectClass(Class<? extends T> clazz){
|
||||
setObjectClass(clazz.getName());
|
||||
}
|
||||
|
||||
protected void setObjectClass(String clazz){
|
||||
if (this.type == null || !this.type.equals(type)) {
|
||||
setObject(null);
|
||||
this.type = clazz;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ import java.util.Map;
|
|||
*/
|
||||
public class HttpRedirectPage implements HttpPage{
|
||||
|
||||
private boolean permanent;
|
||||
private String redirectUrl;
|
||||
|
||||
|
||||
|
|
@ -19,6 +20,11 @@ public class HttpRedirectPage implements HttpPage{
|
|||
this.redirectUrl = redirectUrl;
|
||||
}
|
||||
|
||||
public void setPermanentRedirect(boolean permanent){
|
||||
this.permanent = permanent;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void respond(HttpPrintStream out,
|
||||
HttpHeader headers,
|
||||
|
|
@ -26,7 +32,7 @@ public class HttpRedirectPage implements HttpPage{
|
|||
Map<String, String> cookie,
|
||||
Map<String, String> request) throws IOException {
|
||||
|
||||
out.setStatusCode(301);
|
||||
out.setStatusCode((permanent ? 301 : 307));
|
||||
out.setHeader("Location", redirectUrl);
|
||||
out.print(
|
||||
"<!DOCTYPE HTML>\n" +
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ import java.util.logging.Logger;
|
|||
* fields that should be configurable. And then the gui can use the {@link Configurator#getConfiguration()}
|
||||
* to display all the correct fields. To later save the user input back to the target
|
||||
* object the {@link Configurator#setValues(DataNode)} or {@link Configurator#setValues(Map)}
|
||||
* can be used to set the individual fields and finaly call {@link Configurator#applyConfiguration()}
|
||||
* can be used to set the individual fields and finally call {@link Configurator#applyConfiguration()}
|
||||
* to configure the target object.
|
||||
*
|
||||
* <br>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue