diff --git a/src/zutil/db/bean/DBBean.java b/src/zutil/db/bean/DBBean.java index 78578f6..2564d2a 100755 --- a/src/zutil/db/bean/DBBean.java +++ b/src/zutil/db/bean/DBBean.java @@ -58,9 +58,9 @@ import java.util.logging.Logger; * *Double * *String * *Character - * *DBBean - * *java.sql.Timestamp - * *List + * *java.sql.Timestamp + * *DBBean (A Integer reference to another Bean in another table) + * *List (A reference table is used to associate Beans into the list) * * @author Ziver */ diff --git a/src/zutil/db/bean/DBBeanObjectDSO.java b/src/zutil/db/bean/DBBeanObjectDSO.java new file mode 100755 index 0000000..9dd916e --- /dev/null +++ b/src/zutil/db/bean/DBBeanObjectDSO.java @@ -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: + * + */ +public abstract class DBBeanObjectDSO 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 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 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 clazz){ + setObjectClass(clazz.getName()); + } + + protected void setObjectClass(String clazz){ + if (this.type == null || !this.type.equals(type)) { + setObject(null); + this.type = clazz; + } + } + + +} diff --git a/src/zutil/net/http/page/HttpRedirectPage.java b/src/zutil/net/http/page/HttpRedirectPage.java index 9fc1839..25b894e 100755 --- a/src/zutil/net/http/page/HttpRedirectPage.java +++ b/src/zutil/net/http/page/HttpRedirectPage.java @@ -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 cookie, Map request) throws IOException { - out.setStatusCode(301); + out.setStatusCode((permanent ? 301 : 307)); out.setHeader("Location", redirectUrl); out.print( "\n" + diff --git a/src/zutil/ui/Configurator.java b/src/zutil/ui/Configurator.java index 0920b42..5681a73 100755 --- a/src/zutil/ui/Configurator.java +++ b/src/zutil/ui/Configurator.java @@ -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. * *