KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > content > TreeOperationResult


1 package org.jahia.content;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.ListIterator JavaDoc;
5
6 /**
7  * <p>Title: This class contains the results of a recursive tree
8  * operation.</p>
9  * <p>Description: This class is used directly or derived to store the
10  * results of operations that occur on a tree, such as a content activation
11  * or a content version restore. </p>
12  * <p>Copyright: Copyright (c) 2002</p>
13  * <p>Company: </p>
14  * @author Serge Huber
15  * @version 1.0
16  * @todo implement a way to migrate errors into warnings when actually treating
17  * an error in code.
18  */

19
20 public class TreeOperationResult {
21
22     private static org.apache.log4j.Logger logger =
23             org.apache.log4j.Logger.getLogger(TreeOperationResult.class);
24
25     static final public int FAILED_OPERATION_STATUS = 0;
26     static final public int COMPLETED_OPERATION_STATUS = 1;
27     static final public int PARTIAL_OPERATION_STATUS = 2;
28
29     // The purpose of this matrix is to determine the resulting status. The
30
// hardcoded values correspond to the operation status constants above.
31
private static final int[][] mergeMatrix = {
32         { FAILED_OPERATION_STATUS, FAILED_OPERATION_STATUS, FAILED_OPERATION_STATUS },
33         { FAILED_OPERATION_STATUS, COMPLETED_OPERATION_STATUS, PARTIAL_OPERATION_STATUS },
34         { FAILED_OPERATION_STATUS, PARTIAL_OPERATION_STATUS, PARTIAL_OPERATION_STATUS }
35         };
36
37     // This structure is used to store messages that are non fatal.
38
ArrayList JavaDoc warnings = new ArrayList JavaDoc();
39
40     // This structure is used to store messages that are fatal
41
ArrayList JavaDoc errors = new ArrayList JavaDoc();
42
43     // always look on the bright side :) Actually we need this default
44
// in order to make the matrix merging successful more often.
45
int status = COMPLETED_OPERATION_STATUS;
46
47     public TreeOperationResult() {
48     }
49
50     public TreeOperationResult(int initialStatus) {
51         status = initialStatus;
52     }
53
54     public void appendWarning(Object JavaDoc warning) {
55         warnings.add(warning);
56     }
57
58     public void appendError(Object JavaDoc error) {
59         errors.add(error);
60     }
61
62     public ArrayList JavaDoc getWarnings() {
63         return warnings;
64     }
65
66     public ArrayList JavaDoc getErrors() {
67         return errors;
68     }
69
70     public ListIterator JavaDoc getWarningIterator() {
71         return warnings.listIterator();
72     }
73
74     public ListIterator JavaDoc getErrorIterator() {
75         return errors.listIterator();
76     }
77
78     public int getStatus() {
79         return status;
80     }
81
82     public void setStatus(int newStatus) {
83         status = newStatus;
84     }
85
86     public String JavaDoc toString() {
87         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
88         switch (status) {
89             case FAILED_OPERATION_STATUS :
90                 result.append("Status=FAILED ");
91                 break;
92             case COMPLETED_OPERATION_STATUS :
93                 result.append("Status=COMPLETED ");
94                 break;
95             case PARTIAL_OPERATION_STATUS :
96                 result.append("Status=PARTIAL ");
97                 break;
98         }
99         result.append(" errors : [");
100         int count = 0;
101         ListIterator JavaDoc errorIter = errors.listIterator();
102         while (errorIter.hasNext()) {
103             count++;
104             Object JavaDoc curError = errorIter.next();
105             if (count < errors.size()) {
106                 result.append(curError.toString() + " , ");
107             }
108         }
109         result.append("]");
110
111         result.append(" warnings : [");
112         count = 0;
113         ListIterator JavaDoc warningIter = warnings.listIterator();
114         while (warningIter.hasNext()) {
115             count++;
116             Object JavaDoc curWarning = warningIter.next();
117             if (count < warnings.size()) {
118                 result.append(curWarning.toString() + " , ");
119             }
120         }
121         result.append("]");
122
123         return result.toString();
124     }
125
126     public void merge(TreeOperationResult input) {
127         if (input == null) {
128             logger.debug("Error : merging recursive operation results with null object ! Resulting status is failed !");
129             status = FAILED_OPERATION_STATUS;
130             return;
131         }
132         status = mergeMatrix[input.getStatus()][getStatus()];
133         warnings.addAll(input.getWarnings());
134         errors.addAll(input.getErrors());
135     }
136
137     /**
138      * This method migrates all the errors to warnings.
139      */

140     public void moveErrorsToWarnings() {
141         warnings.addAll(errors);
142         errors.clear();
143     }
144
145 }
Popular Tags