001 /*
002 *
003 * $Revision: 15591 $ $Date: 2009-07-23 13:11:59 +0200 (Thu, 23 Jul 2009) $
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.services.fieldquery;
025
026 import org.jdom.Element;
027 import org.mycore.common.MCRConfigurationException;
028 import org.mycore.common.MCRConstants;
029 import org.mycore.common.MCRException;
030 import org.mycore.common.MCRNormalizer;
031 import org.mycore.datamodel.ifs.MCRFile;
032
033 /**
034 * Represents the value of a field in a query. This can be a value that is part
035 * of the query results (hit sort data or meta data) or a value that is built
036 * when data is indexed.
037 *
038 * @author Frank Lützenkirchen
039 */
040 public class MCRFieldValue {
041
042 /**
043 * The field this value belongs to
044 */
045 private MCRFieldDef field;
046
047 /**
048 * The fields's value, as a String
049 */
050 private String value;
051
052 /**
053 * The MCRFile thats content should become the value of the field when
054 * indexing data
055 */
056 private MCRFile file;
057
058 /**
059 * Creates a new field value
060 *
061 * @param field
062 * the field this value belongs to
063 * @param value
064 * the value of the field, as a String
065 */
066 public MCRFieldValue(MCRFieldDef field, String value) {
067 if (field == null)
068 throw new NullPointerException("MCRFieldDef cannot be null.");
069 this.field = field;
070 setValue(value);
071 }
072
073 /**
074 * Creates a new field value
075 *
076 * @param field
077 * the field this value belongs to
078 * @param file
079 * the MCRFile thats content should become the value of the field
080 * when indexing data
081 */
082 MCRFieldValue(MCRFieldDef field, MCRFile file) {
083 if (field == null)
084 throw new NullPointerException("MCRFieldDef cannot be null.");
085 this.field = field;
086 this.file = file;
087 }
088
089 /**
090 * Returns the field this value belongs to
091 */
092 public MCRFieldDef getField() {
093 return field;
094 }
095
096 /**
097 * Sets or updates the field value
098 * @param value the value, whicht will be normalized
099 */
100 public void setValue(String value) {
101 if (field.getDataType().equals("text") || field.getDataType().equals("name"))
102 this.value = MCRNormalizer.normalizeString(value);
103 if (field.getDataType().equals("decimal"))
104 this.value = value.replace(',','.');
105 else
106 this.value = value;
107 }
108
109 /**
110 * Returns the value of the field as a String
111 *
112 * @return the value of the field as a String, or null if the value is the
113 * content of an MCRFile
114 * @see #getFile()
115 */
116 public String getValue() {
117 return value;
118 }
119
120 /**
121 * Returns the MCRFile thats content should become the value of the field
122 * when indexing data
123 *
124 * @return the MCRFile to be indexed, or null if the value can be retrieved
125 * using the getValue() method
126 * @see #getValue()
127 */
128 public MCRFile getFile() {
129 return file;
130 }
131
132 /**
133 * Builds a XML representation of this field's value
134 *
135 * @return a 'field' element with attribute 'name' and the value as element
136 * content
137 */
138 public Element buildXML() {
139 Element eField = new Element("field", MCRConstants.MCR_NAMESPACE);
140 eField.setAttribute("name", field.getName());
141 eField.addContent(value);
142 return eField;
143 }
144
145 /**
146 * Parses a XML representation of a field value
147 *
148 * @param xml
149 * the field value as XML element
150 * @return the parsed MCRFieldValue object
151 */
152 public static MCRFieldValue parseXML(Element xml) {
153 String name = xml.getAttributeValue("name", "");
154 String value = xml.getText();
155
156 if (name.length() == 0)
157 throw new MCRException("Field value attribute 'name' is empty");
158 if (value.length() == 0)
159 throw new MCRException("Field value is empty");
160
161 return new MCRFieldValue(MCRFieldDef.getDef(name), value);
162 }
163
164 public String toString() {
165 return this.field.getName() + " = " + this.value;
166 }
167 }