001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.form; 017 018import java.util.Arrays; 019import java.util.Date; 020import java.util.LinkedHashMap; 021import java.util.Map; 022import java.util.Set; 023 024import org.ametys.cms.repository.Content; 025import org.ametys.core.user.UserIdentity; 026import org.ametys.plugins.repository.metadata.MetadataComment; 027 028/** 029 * Abstraction of the form submitted by the client. 030 */ 031public class Form 032{ 033 private final Map<String, Object> _fields = new LinkedHashMap<>(); 034 private final Map<String, MetadataComment[]> _comments = new LinkedHashMap<>(); 035 036 /** 037 * Returns an array containing field names. 038 * @return an array containing field names. 039 */ 040 public Set<String> getFieldNames() 041 { 042 return _fields.keySet(); 043 } 044 045 /** 046 * Returns the named field's value as {@link Form}. 047 * @param fieldName the field name. 048 * @return the field value as {@link Form}. 049 */ 050 public Form getCompositeField(String fieldName) 051 { 052 Object value = _fields.get(fieldName); 053 054 if (value == null) 055 { 056 return null; 057 } 058 059 return (Form) value; 060 } 061 062 /** 063 * Set a composite field. 064 * @param fieldName the field name. 065 * @param compositeField the composite field. 066 */ 067 public void setCompositeField(String fieldName, Form compositeField) 068 { 069 _fields.put(fieldName, compositeField); 070 } 071 072 /** 073 * Returns the named field's value as {@link RepeaterField}. 074 * @param fieldName the field name. 075 * @return the field value as {@link RepeaterField}. 076 */ 077 public RepeaterField getRepeaterField(String fieldName) 078 { 079 Object value = _fields.get(fieldName); 080 081 if (value == null) 082 { 083 return null; 084 } 085 086 return (RepeaterField) value; 087 } 088 089 /** 090 * Returns the named field's value as {@link RichTextField}. 091 * @param fieldName the field name. 092 * @return the field value as RichText. 093 */ 094 public RichTextField getRichTextField(String fieldName) 095 { 096 Object value = _fields.get(fieldName); 097 098 if (value == null) 099 { 100 return null; 101 } 102 103 if (value instanceof ExternalizableField) 104 { 105 return (RichTextField) ((ExternalizableField) value).getLocalField(); 106 } 107 else 108 { 109 return (RichTextField) value; 110 } 111 } 112 113 /** 114 * Returns the named field's value as {@link BinaryField}. 115 * @param fieldName the field name. 116 * @return the field value as a BinaryField. 117 */ 118 public BinaryField getBinaryField(String fieldName) 119 { 120 Object value = _fields.get(fieldName); 121 122 if (value == null) 123 { 124 return null; 125 } 126 127 if (value instanceof ExternalizableField) 128 { 129 return (BinaryField) ((ExternalizableField) value).getLocalField(); 130 } 131 else 132 { 133 return (BinaryField) value; 134 } 135 } 136 137 /** 138 * Returns the named field's value as {@link SubContentField}. 139 * @param fieldName the field name. 140 * @return the field value as a SubContentField. 141 */ 142 public SubContentField getSubContentField(String fieldName) 143 { 144 Object value = _fields.get(fieldName); 145 146 if (value == null) 147 { 148 return null; 149 } 150 151 if (value instanceof ExternalizableField) 152 { 153 return (SubContentField) ((ExternalizableField) value).getLocalField(); 154 } 155 else 156 { 157 return (SubContentField) value; 158 } 159 } 160 161 /** 162 * Returns the named field's value as {@link ReferenceField}. 163 * @param fieldName the field name. 164 * @return the field value as a ReferenceField. 165 */ 166 public ReferenceField getReferenceField(String fieldName) 167 { 168 Object value = _fields.get(fieldName); 169 170 if (value == null) 171 { 172 return null; 173 } 174 175 if (value instanceof ExternalizableField) 176 { 177 return (ReferenceField) ((ExternalizableField) value).getLocalField(); 178 } 179 else 180 { 181 return (ReferenceField) value; 182 } 183 } 184 185 /** 186 * Returns the named field's value as String array. 187 * @param fieldName the field name. 188 * @return field value as String array. 189 */ 190 @SuppressWarnings("unchecked") 191 public SimpleField<String> getStringArray(String fieldName) 192 { 193 Object value = _fields.get(fieldName); 194 195 if (value == null) 196 { 197 return null; 198 } 199 200 if (value instanceof ExternalizableField) 201 { 202 return (SimpleField<String>) ((ExternalizableField) value).getLocalField(); 203 } 204 else 205 { 206 return (SimpleField<String>) value; 207 } 208 } 209 210 /** 211 * Returns the named field's value as Date array. 212 * @param fieldName the field name. 213 * @return field value as Date array. 214 */ 215 @SuppressWarnings("unchecked") 216 public SimpleField<Date> getDateArray(String fieldName) 217 { 218 Object value = _fields.get(fieldName); 219 220 if (value == null) 221 { 222 return null; 223 } 224 225 if (value instanceof ExternalizableField) 226 { 227 return (SimpleField<Date>) ((ExternalizableField) value).getLocalField(); 228 } 229 else 230 { 231 return (SimpleField<Date>) value; 232 } 233 } 234 235 /** 236 * Returns the named field's value as long array. 237 * @param fieldName the field name. 238 * @return field value as long array. 239 */ 240 @SuppressWarnings("unchecked") 241 public SimpleField<Long> getLongArray(String fieldName) 242 { 243 Object value = _fields.get(fieldName); 244 245 if (value == null) 246 { 247 return null; 248 } 249 250 if (value instanceof ExternalizableField) 251 { 252 return (SimpleField<Long>) ((ExternalizableField) value).getLocalField(); 253 } 254 else 255 { 256 return (SimpleField<Long>) value; 257 } 258 } 259 260 /** 261 * Returns the named field's value as double array. 262 * @param fieldName the field name. 263 * @return field value as double array. 264 */ 265 @SuppressWarnings("unchecked") 266 public SimpleField<Double> getDoubleArray(String fieldName) 267 { 268 Object value = _fields.get(fieldName); 269 270 if (value == null) 271 { 272 return null; 273 } 274 275 if (value instanceof ExternalizableField) 276 { 277 return (SimpleField<Double>) ((ExternalizableField) value).getLocalField(); 278 } 279 else 280 { 281 return (SimpleField<Double>) value; 282 } 283 } 284 285 /** 286 * Returns the named field's value as boolean array. 287 * @param fieldName the field name. 288 * @return field value as boolean array. 289 */ 290 @SuppressWarnings("unchecked") 291 public SimpleField<Boolean> getBooleanArray(String fieldName) 292 { 293 Object value = _fields.get(fieldName); 294 295 if (value == null) 296 { 297 return null; 298 } 299 300 if (value instanceof ExternalizableField) 301 { 302 return (SimpleField<Boolean>) ((ExternalizableField) value).getLocalField(); 303 } 304 else 305 { 306 return (SimpleField<Boolean>) value; 307 } 308 } 309 310 /** 311 * Returns the named field's value as UserIdentity array. 312 * @param fieldName the field name. 313 * @return field value as UserIdentity array. 314 */ 315 @SuppressWarnings("unchecked") 316 public SimpleField<UserIdentity> getUserArray(String fieldName) 317 { 318 Object value = _fields.get(fieldName); 319 320 if (value == null) 321 { 322 return null; 323 } 324 325 if (value instanceof ExternalizableField) 326 { 327 return (SimpleField<UserIdentity>) ((ExternalizableField) value).getLocalField(); 328 } 329 else 330 { 331 return (SimpleField<UserIdentity>) value; 332 } 333 } 334 335 /** 336 * Returns the named field's value as a content array. 337 * @param fieldName the field name. 338 * @return the field value as a content array. 339 */ 340 @SuppressWarnings("unchecked") 341 public SimpleField<Content> getContentArray(String fieldName) 342 { 343 Object value = _fields.get(fieldName); 344 345 if (value == null) 346 { 347 return null; 348 } 349 350 if (value instanceof ExternalizableField) 351 { 352 return (SimpleField<Content>) ((ExternalizableField) value).getLocalField(); 353 } 354 else 355 { 356 return (SimpleField<Content>) value; 357 } 358 } 359 360 /** 361 * Returns the named field's value as a {@link ExternalizableField} 362 * @param fieldName the field name. 363 * @return the externalizable field 364 */ 365 public ExternalizableField getExternalizableField(String fieldName) 366 { 367 Object value = _fields.get(fieldName); 368 369 if (value == null) 370 { 371 return null; 372 } 373 374 return (ExternalizableField) value; 375 } 376 377 /** 378 * Set a multi-valued date field. 379 * @param fieldName the field name. 380 * @param values the Date array containing values of this field. 381 */ 382 public void setField(String fieldName, AbstractField values) 383 { 384 _fields.put(fieldName, values); 385 } 386 387 /** 388 * Get a non-typed field. 389 * @param fieldName The name of the field 390 * @return The desired field or null 391 */ 392 public AbstractField getField(String fieldName) 393 { 394 return (AbstractField) _fields.get(fieldName); 395 } 396 397 /** 398 * Return the array of comments for the named field. 399 * @param metadataName the field name. 400 * @return the array of comments 401 */ 402 public MetadataComment[] getCommentArray(String metadataName) 403 { 404 return _comments.get(metadataName); 405 } 406 407 /** 408 * Set the comment of a field 409 * @param fieldName The field name 410 * @param comments The array of metadata comments 411 */ 412 public void setCommentsField(String fieldName, MetadataComment[] comments) 413 { 414 _comments.put(fieldName, comments); 415 } 416 417 @Override 418 public String toString() 419 { 420 StringBuilder fields = new StringBuilder(); 421 422 if (_fields.isEmpty()) 423 { 424 fields.append("EMPTY"); 425 fields.append(System.getProperty("line.separator")); 426 } 427 428 for (Map.Entry<String, Object> entry : _fields.entrySet()) 429 { 430 fields.append(entry.getKey()); 431 fields.append(": "); 432 433 fields.append(entry.getValue()); 434 435 fields.append(System.getProperty("line.separator")); 436 } 437 438 for (Map.Entry<String, MetadataComment[]> entry : _comments.entrySet()) 439 { 440 MetadataComment[] comments = entry.getValue(); 441 442 fields.append(entry.getKey() + " (comments)"); 443 fields.append(": "); 444 fields.append(Arrays.asList(comments)); 445 fields.append(System.getProperty("line.separator")); 446 } 447 448 return fields.toString(); 449 } 450}