1 /* 2 * $HeadURL: $ 3 * $Date: $ 4 * $Revision: $ 5 * $Author: $ 6 * 7 * Copyright (c) 2006 MindTree Consulting Ltd. 8 * 9 * This file is part of Insight. 10 * 11 * Insight is free software: you can redistribute it and/or modify it under the 12 * terms of the GNU General Public License as published by the Free Software 13 * Foundation, either version 3 of the License, or (at your option) any later 14 * version. 15 * 16 * Insight is distributed in the hope that it will be useful, but 17 * WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 19 * Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License along with 22 * Insight. If not, see <http://www.gnu.org/licenses/>. 23 */ 24 package com.mindtree.techworks.insight.reporting.verifiers; 25 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 import java.util.Collection; 29 import java.util.Iterator; 30 import java.util.List; 31 import java.util.Map; 32 33 import com.mindtree.techworks.insight.reporting.SerializationXmlUtils; 34 import com.mindtree.techworks.insight.reporting.jobs.Job; 35 import com.mindtree.techworks.insight.reporting.jobs.JobPersistanceHandler; 36 37 /** 38 * An abstract implementation of the <code>Verifier</code> interface. It 39 * provides common services required by verifiers at a single location. Some 40 * of those include maintaining a registry of <code>Job</code>s currently registered to be 41 * called when this <code>Verifier</code> is triggered. 42 * 43 * @see com.mindtree.techworks.insight.reporting.verifiers.Verifier 44 * @see com.mindtree.techworks.insight.reporting.jobs.Job 45 * 46 * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a> 47 * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $ 48 * @since Insight 1.5 49 */ 50 public abstract class AbstractVerifier implements Verifier, 51 DefaultImplDeserializable { 52 53 /** 54 * Used for object serialization 55 */ 56 private static final long serialVersionUID = 5690358613929479728L; 57 58 // ------------------------------------------------------------------------- 59 // Instance Fields 60 // ------------------------------------------------------------------------- 61 62 /** 63 * The lists of jobs to be invoked when the verifier is triggered. 64 */ 65 protected List _jobs = null; 66 67 /** 68 * The display name of the <code>Verifier</code>. 69 */ 70 private String _displayName = null; 71 72 // ------------------------------------------------------------------------- 73 // Constructor(s) 74 // ------------------------------------------------------------------------- 75 76 /** 77 * Creates a new <code>AbstractVerifier</code> instance, with the display 78 * name provided. 79 * 80 * @param displayName The display name of the <code>Verifier</code>. 81 */ 82 public AbstractVerifier (String displayName) { 83 this._displayName = displayName; 84 } 85 86 /** 87 * Creates a new <code>AbstractVerifier</code> instance. 88 */ 89 public AbstractVerifier () { 90 // Default constructor 91 } 92 93 // ------------------------------------------------------------------------- 94 // Abstract methods to be implemented by extending types 95 // ------------------------------------------------------------------------- 96 97 /** 98 * The implementing verifiers should return a map of the fields that they 99 * require to be serialized as a map. All field values need to have proper 100 * implementation of the <code>toString()</code> method, or they need to be 101 * arrays or <code>Collection</code>s. In the latter case, the values in the 102 * array or <code>Collection</code> need to return serializable data from 103 * the <code>toString()</code> method. 104 * 105 * @return A map containing the name and value pairs of each of the fields. 106 */ 107 protected abstract Map getFieldMap(); 108 109 /** 110 * Returns <code>true</code> if the <code>Job</code>s registered for 111 * this <code>Verifier</code> are to be serialized. 112 * 113 * @return <code>true</code> if <code>Job</code>s are to be serialized 114 * or <code>false</code> 115 */ 116 protected abstract boolean serializeJobs(); 117 118 // ------------------------------------------------------------------------- 119 // Methods implemented from com.mindtree.techworks.insight.reporting.verifiers.Verifier 120 // ------------------------------------------------------------------------- 121 122 /** 123 * Registers a job to be executed when the Verifier is triggered. 124 * 125 * @see com.mindtree.techworks.insight.reporting.verifiers.Verifier#registerJob(com.mindtree.techworks.insight.reporting.jobs.Job) 126 * 127 * @param job The <code>Job</code> to be registered. 128 * @throws IllegalArgumentException If the job passed in is <code>null</code> 129 */ 130 public void registerJob (Job job) { 131 132 // Check if the job is not null 133 if (null == job) { 134 throw new IllegalArgumentException ("Job to be registered cannot be null"); 135 } 136 137 // If this is the first job to be added, initialize the list. 138 if (null == _jobs) { 139 _jobs = new ArrayList(); 140 } 141 142 _jobs.add(job); 143 } 144 145 /** 146 * Generates a default serialized form of the <code>Verifier</code>. Only 147 * fields returned by the {@link #getFieldMap() #getFieldMap()} method are 148 * serialized. <code>Verifiers</code> serialized using this method may be 149 * deserialized using the <code>VerifierDeserializerImpl</code>. 150 * <p> 151 * For a format of the serialized <code>Verifier</code> see 152 * {@link VerifierDeserializer VerifierDeserializer} documentation. 153 * </p> 154 * <p> 155 * <code>Verifiers</code> extending this class may override this method and 156 * provide their own <code>VerifierDeserializer</code> instances. 157 * </p> 158 * 159 * @see com.mindtree.techworks.insight.reporting.verifiers.Verifier#serialize() 160 */ 161 public String serialize () { 162 163 StringBuffer serializedForm = new StringBuffer(); 164 serializedForm.append("<verifier>"); 165 166 // The implementation class 167 serializedForm.append("<class>"); 168 serializedForm.append(this.getClass().getName()); 169 serializedForm.append("</class>"); 170 171 // The type of the verifier 172 serializedForm.append("<name>"); 173 serializedForm.append(this.getDisplayName()); 174 serializedForm.append("</name>"); 175 176 // Verifier fields 177 serializedForm.append("<fields>"); 178 Map fields = this.getFieldMap(); 179 for (Iterator fieldItr = fields.keySet().iterator(); fieldItr.hasNext();) { 180 String fieldName = (String) fieldItr.next(); 181 Object fieldValue = fields.get(fieldName); 182 boolean isArray = false; 183 184 // Check if the field is an array 185 if (fieldValue instanceof Collection || fieldValue.getClass().isArray()) { 186 isArray = true; 187 } 188 189 // Write the field: 190 serializedForm.append("<field>"); 191 192 // Name of the field 193 serializedForm.append("<name>"); 194 serializedForm.append(fieldName); 195 serializedForm.append("</name>"); 196 197 serializedForm.append("<isArray>"); 198 serializedForm.append(isArray); 199 serializedForm.append("</isArray>"); 200 201 if (!isArray) { 202 serializedForm.append("<value>"); 203 serializedForm.append (SerializationXmlUtils 204 .escapeSpecialChars (fieldValue.toString ())); 205 serializedForm.append("<value>"); 206 } else { 207 // Field value is an array, so you will have multiple value tags 208 Collection values = null; 209 // If it is an array get the array values in a collection. 210 if (fieldValue.getClass().isArray()) { 211 values = Arrays.asList((Object []) fieldValue); 212 } else { 213 values = (Collection) fieldValue; 214 } 215 216 for (Iterator valItr = values.iterator(); valItr.hasNext(); ) { 217 serializedForm.append("<value>"); 218 serializedForm.append (SerializationXmlUtils 219 .escapeSpecialChars (valItr.next ().toString ())); 220 serializedForm.append("<value>"); 221 } 222 } 223 224 serializedForm.append("</field>"); 225 } 226 serializedForm.append("</fields>"); 227 228 // Serialize the jobs - if the extending class wants them to be 229 // serialized. 230 serializedForm.append("<jobs>"); 231 if (null != _jobs && serializeJobs()) { 232 for (Iterator jobsItr = _jobs.iterator(); jobsItr.hasNext(); ) { 233 Job job = (Job) jobsItr.next(); 234 serializedForm.append(JobPersistanceHandler.serializeJob(job)); 235 } 236 } 237 serializedForm.append("</jobs>"); 238 239 serializedForm.append("</verifier>"); 240 241 return serializedForm.toString(); 242 } 243 244 /** 245 * Returns the default implementation of <code>VerifierDeserializer</code>. 246 * Subclasses using a different <code>VerifierDeserializer</code> 247 * implementation should override this method. 248 * 249 * @see com.mindtree.techworks.insight.reporting.verifiers.Verifier#getDeserializer() 250 */ 251 public VerifierDeserializer getDeserializer () { 252 253 return new VerifierDeserializerImpl(); 254 } 255 256 /** 257 * @see com.mindtree.techworks.insight.reporting.verifiers.Verifier#getDisplayName() 258 */ 259 public String getDisplayName() { 260 261 return this._displayName; 262 } 263 264 /** 265 * @see com.mindtree.techworks.insight.reporting.verifiers.Verifier#getUID() 266 */ 267 public String getUID() { 268 269 return this.getClass().getName(); 270 } 271 272 // ------------------------------------------------------------------------- 273 // Accessor methods 274 // ------------------------------------------------------------------------- 275 276 /** 277 * Sets the display name of the <code>Verifier</code>. Sub classes, which do 278 * not use the constructor to set the <i>display name</i> of the 279 * <code>Verifier</code>, may use this method to set the display name. 280 * 281 * @param displayName The display name of the <code>Verifier</code>. 282 */ 283 protected void setDisplayName (String displayName) { 284 this._displayName = displayName; 285 } 286 }