comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 3f24865082da
children f46caf95ab7f
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.model;
2
3 import org.w3c.dom.Document;
4 import org.w3c.dom.Element;
5
6 import java.util.List;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9
10 import java.io.Serializable;
11
12 import de.intevation.artifacts.CallMeta;
13
14 import de.intevation.flys.artifacts.resources.Resources;
15
16 import de.intevation.flys.utils.Formatter;
17
18 public class Calculation
19 implements Serializable
20 {
21 public static class Problem
22 implements Serializable
23 {
24 protected Double km;
25 protected String msg;
26 protected Object [] args;
27
28 public Problem() {
29 }
30
31 public Problem(String msg) {
32 this.msg = msg;
33 }
34
35 public Problem(String msg, Object [] args) {
36 this.msg = msg;
37 this.args = args;
38 }
39
40 public Problem(double km, String msg) {
41 this.km = km;
42 this.msg = msg;
43 }
44
45 public Problem(double km, String msg, Object [] args) {
46 this.km = km;
47 this.msg = msg;
48 this.args = args;
49 }
50
51 public Element toXML(Document document, CallMeta meta) {
52 Element problem = document.createElement("problem");
53 if (km != null) {
54 problem.setAttribute(
55 "km",
56 Formatter.getCalculationKm(meta).format(km));
57 }
58 String text = args != null
59 ? Resources.getMsg(meta, msg, msg, args)
60 : Resources.getMsg(meta, msg, msg);
61 problem.setTextContent(text);
62 return problem;
63 }
64
65 @Override
66 public boolean equals(Object other) {
67 if (!(other instanceof Problem)) {
68 return false;
69 }
70 Problem o = (Problem)other;
71 return !(!msg.equals(o.msg)
72 || (km == null && o.km != null)
73 || (km != null && o.km == null)
74 || (km != null && !km.equals(o.km))
75 || !Arrays.equals(args, o.args));
76 }
77 } // class Problem
78
79 protected List<Problem> problems;
80
81 public Calculation() {
82 }
83
84 public Calculation(String msg) {
85 addProblem(msg);
86 }
87
88 protected List<Problem> checkProblems() {
89 if (problems == null) {
90 problems = new ArrayList<Problem>();
91 }
92 return problems;
93 }
94
95 public void addProblems(Calculation other) {
96 List<Problem> otherProblems = other.problems;
97 if (otherProblems != null) {
98 List<Problem> problems = checkProblems();
99 for (Problem problem: otherProblems) {
100 if (!problems.contains(problem)) {
101 problems.add(problem);
102 }
103 }
104 }
105 }
106
107 public void addProblem(Problem problem) {
108 List<Problem> problems = checkProblems();
109 if (!problems.contains(problem)) {
110 problems.add(problem);
111 }
112 }
113
114 public void addProblem(String msg) {
115 addProblem(new Problem(msg));
116 }
117
118 public void addProblem(String msg, Object ... args) {
119 addProblem(new Problem(msg, args));
120 }
121
122 public void addProblem(double km, String msg) {
123 addProblem(new Problem(km, msg));
124 }
125
126 public void addProblem(double km, String msg, Object ... args) {
127 addProblem(new Problem(km, msg, args));
128 }
129
130 public boolean hasProblems() {
131 return problems != null && !problems.isEmpty();
132 }
133
134 public int numProblems() {
135 return problems != null ? problems.size() : 0;
136 }
137
138 public List<Problem> getProblems() {
139 return problems;
140 }
141
142 public void toXML(Document document, CallMeta meta) {
143
144 Element root = document.createElement("problems");
145
146 if (hasProblems()) {
147 for (Problem problem: problems) {
148 root.appendChild(problem.toXML(document, meta));
149 }
150 }
151
152 document.appendChild(root);
153 }
154 }
155 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org