ingo@686: package de.intevation.flys.artifacts.model; ingo@686: ingo@686: import org.w3c.dom.Document; ingo@686: import org.w3c.dom.Element; ingo@686: ingo@686: import java.util.List; ingo@686: import java.util.ArrayList; sascha@2166: import java.util.Arrays; ingo@686: ingo@686: import java.io.Serializable; ingo@686: sascha@710: import de.intevation.artifacts.CallMeta; sascha@710: sascha@2166: import de.intevation.flys.artifacts.resources.Resources; sascha@2166: sascha@2290: import de.intevation.flys.utils.Formatter; sascha@2290: felix@4274: /** A calculation that can have result and problems. */ ingo@686: public class Calculation ingo@686: implements Serializable ingo@686: { felix@4274: /** Problem of a calculation. felix@4274: * Has location and message. */ sascha@742: public static class Problem ingo@686: implements Serializable ingo@686: { sascha@2166: protected Double km; sascha@2166: protected String msg; sascha@2166: protected Object [] args; ingo@686: ingo@686: public Problem() { ingo@686: } ingo@686: ingo@686: public Problem(String msg) { ingo@686: this.msg = msg; ingo@686: } ingo@686: sascha@2166: public Problem(String msg, Object [] args) { sascha@2166: this.msg = msg; sascha@2166: this.args = args; sascha@2166: } sascha@2166: ingo@686: public Problem(double km, String msg) { ingo@686: this.km = km; ingo@686: this.msg = msg; ingo@686: } ingo@686: sascha@2166: public Problem(double km, String msg, Object [] args) { sascha@2166: this.km = km; sascha@2166: this.msg = msg; sascha@2166: this.args = args; sascha@2166: } sascha@2166: sascha@710: public Element toXML(Document document, CallMeta meta) { ingo@686: Element problem = document.createElement("problem"); ingo@686: if (km != null) { sascha@2290: problem.setAttribute( sascha@2290: "km", sascha@2290: Formatter.getCalculationKm(meta).format(km)); ingo@686: } sascha@2166: String text = args != null sascha@2211: ? Resources.getMsg(meta, msg, msg, args) sascha@2211: : Resources.getMsg(meta, msg, msg); sascha@2166: problem.setTextContent(text); ingo@686: return problem; ingo@686: } sascha@2166: felix@4275: public String getMsg() { felix@4275: return msg; felix@4275: } felix@4275: sascha@2166: @Override sascha@2166: public boolean equals(Object other) { sascha@2166: if (!(other instanceof Problem)) { sascha@2166: return false; sascha@2166: } sascha@2166: Problem o = (Problem)other; sascha@2166: return !(!msg.equals(o.msg) sascha@2166: || (km == null && o.km != null) sascha@2166: || (km != null && o.km == null) sascha@2166: || (km != null && !km.equals(o.km)) sascha@2166: || !Arrays.equals(args, o.args)); sascha@2166: } ingo@686: } // class Problem ingo@686: ingo@686: protected List problems; ingo@686: ingo@686: public Calculation() { ingo@686: } ingo@686: sascha@709: public Calculation(String msg) { sascha@709: addProblem(msg); sascha@709: } sascha@709: ingo@686: protected List checkProblems() { ingo@686: if (problems == null) { ingo@686: problems = new ArrayList(); ingo@686: } ingo@686: return problems; ingo@686: } ingo@686: sascha@2624: public void addProblems(Calculation other) { sascha@2624: List otherProblems = other.problems; sascha@2624: if (otherProblems != null) { sascha@2624: List problems = checkProblems(); sascha@2624: for (Problem problem: otherProblems) { sascha@2624: if (!problems.contains(problem)) { sascha@2624: problems.add(problem); sascha@2624: } sascha@2624: } sascha@2624: } sascha@2624: } sascha@2624: sascha@2166: public void addProblem(Problem problem) { sascha@2166: List problems = checkProblems(); sascha@2166: if (!problems.contains(problem)) { sascha@2166: problems.add(problem); sascha@2166: } sascha@2166: } sascha@2166: ingo@686: public void addProblem(String msg) { sascha@2166: addProblem(new Problem(msg)); sascha@2166: } sascha@2166: sascha@2166: public void addProblem(String msg, Object ... args) { sascha@2166: addProblem(new Problem(msg, args)); ingo@686: } ingo@686: ingo@686: public void addProblem(double km, String msg) { sascha@2166: addProblem(new Problem(km, msg)); sascha@2166: } sascha@2166: sascha@2166: public void addProblem(double km, String msg, Object ... args) { sascha@2166: addProblem(new Problem(km, msg, args)); ingo@686: } ingo@686: ingo@686: public boolean hasProblems() { ingo@686: return problems != null && !problems.isEmpty(); ingo@686: } ingo@686: ingo@686: public int numProblems() { ingo@686: return problems != null ? problems.size() : 0; ingo@686: } ingo@686: ingo@686: public List getProblems() { ingo@686: return problems; ingo@686: } ingo@686: sascha@710: public void toXML(Document document, CallMeta meta) { ingo@686: ingo@686: Element root = document.createElement("problems"); ingo@686: ingo@686: if (hasProblems()) { ingo@686: for (Problem problem: problems) { sascha@710: root.appendChild(problem.toXML(document, meta)); ingo@686: } ingo@686: } ingo@686: ingo@686: document.appendChild(root); ingo@686: } ingo@686: } ingo@686: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :