001: /**
002: * LibreSource Community
003: * Copyright (C) 2004-2007 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This software is not a free software; you can modify it under the
007: * LibreSource Enterprise user license but you can't redistribute it.
008: * See licenses details in LSE-user-license.txt
009: *
010: * Initial authors :
011: *
012: * Guillaume Bort / INRIA
013: * Francois Charoy / Universite Nancy 2
014: * Julien Forest / Artenum
015: * Claude Godart / Universite Henry Poincare
016: * Florent Jouille / INRIA
017: * Sebastien Jourdain / INRIA / Artenum
018: * Yves Lerumeur / Artenum
019: * Pascal Molli / Universite Henry Poincare
020: * Gerald Oster / INRIA
021: * Mariarosa Penzi / Artenum
022: * Gerard Sookahet / Artenum
023: * Raphael Tani / INRIA
024: *
025: * Contributors :
026: *
027: * Stephane Bagnier / Artenum
028: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
029: */package org.libresource.form;
030:
031: import java.io.IOException;
032: import java.io.ObjectInputStream;
033: import java.io.ObjectOutputStream;
034: import java.io.Serializable;
035:
036: import java.net.URI;
037:
038: import java.util.ArrayList;
039: import java.util.Collection;
040: import java.util.Hashtable;
041: import java.util.Iterator;
042:
043: /**
044: * Artenum Contribution
045: * @author <a href="mailto:jourdain@artenum.com">Sebastien Jourdain</a> - <a href="http://www.artenum.com">Artenum</a>
046: */
047: public class Form implements Serializable {
048: private static final long serialVersionUID = 1L;
049: private Hashtable<String, FormField> fields;
050: private ArrayList<String> fieldsId;
051: private URI localURI;
052:
053: public Form() {
054: fields = new Hashtable<String, FormField>(10);
055: fieldsId = new ArrayList<String>(10);
056: }
057:
058: public FormField getField(String key) {
059: return (FormField) fields.get(key);
060: }
061:
062: public void addField(FormField field) {
063: fieldsId.add(field.getFieldId());
064: fields.put(field.getFieldId(), field);
065: }
066:
067: public Collection<String> getIds() {
068: return fieldsId;
069: }
070:
071: public Collection<FormField> getFields() {
072: ArrayList<FormField> result = new ArrayList<FormField>();
073: for (String id : fieldsId) {
074: result.add(getField(id));
075: }
076: return result;
077: }
078:
079: public Collection<FormField> getFormFields() {
080: return getFields();
081: }
082:
083: public Collection<String> getFieldsLabel() {
084: ArrayList<String> result = new ArrayList<String>();
085: for (String id : fieldsId) {
086: result.add(getField(id).getFieldLabel());
087: }
088: return result;
089: }
090:
091: public String getLabel(String fieldId) {
092: return getField(fieldId).getFieldLabel();
093: }
094:
095: public String getFieldsId() {
096: StringBuffer result = new StringBuffer();
097:
098: for (Iterator i = fieldsId.iterator(); i.hasNext();) {
099: result.append(getField((String) i.next()).getFieldId());
100:
101: if (i.hasNext()) {
102: result.append(",");
103: }
104: }
105:
106: return result.toString();
107: }
108:
109: public void removeAllFields() {
110: fields.clear();
111: fieldsId.clear();
112: }
113:
114: public String toString() {
115: StringBuffer result = new StringBuffer();
116:
117: for (FormField field : getFields())
118: result.append(field);
119:
120: return result.toString();
121: }
122:
123: public URI getLocalURI() {
124: return localURI;
125: }
126:
127: public void setLocalURI(URI localURI) {
128: this .localURI = localURI;
129: }
130:
131: private void writeObject(ObjectOutputStream out) throws IOException {
132: // Write current version
133: out.writeInt(1);
134:
135: // Write data for V1
136: out.writeObject(fieldsId);
137: out.writeObject(fields);
138: }
139:
140: private void readObject(ObjectInputStream in) throws IOException,
141: ClassNotFoundException {
142: int version = in.readInt();
143: switch (version) {
144: case 1:
145: fieldsId = (ArrayList<String>) in.readObject();
146: fields = (Hashtable<String, FormField>) in.readObject();
147: break;
148: }
149: }
150: }
|