1 package org.jahia.content; 2 3 import java.util.ArrayList ; 4 import java.util.ListIterator ; 5 6 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 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 ArrayList warnings = new ArrayList (); 39 40 ArrayList errors = new ArrayList (); 42 43 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 warning) { 55 warnings.add(warning); 56 } 57 58 public void appendError(Object error) { 59 errors.add(error); 60 } 61 62 public ArrayList getWarnings() { 63 return warnings; 64 } 65 66 public ArrayList getErrors() { 67 return errors; 68 } 69 70 public ListIterator getWarningIterator() { 71 return warnings.listIterator(); 72 } 73 74 public ListIterator 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 toString() { 87 StringBuffer result = new StringBuffer (); 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 errorIter = errors.listIterator(); 102 while (errorIter.hasNext()) { 103 count++; 104 Object 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 warningIter = warnings.listIterator(); 114 while (warningIter.hasNext()) { 115 count++; 116 Object 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 140 public void moveErrorsToWarnings() { 141 warnings.addAll(errors); 142 errors.clear(); 143 } 144 145 } | Popular Tags |