001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.corebuilders;
011:
012: import java.util.*;
013:
014: import org.mmbase.datatypes.DataType;
015: import org.mmbase.bridge.NodeManager;
016: import org.mmbase.datatypes.DataTypes;
017: import org.mmbase.core.CoreField;
018: import org.mmbase.core.util.Fields;
019: import org.mmbase.storage.util.Index;
020:
021: /**
022: * One of the core objects. It is not itself a builder, but is used by builders. Defines one field
023: * of a object type / builder.
024: *
025: * @author Daniel Ockeloen
026: * @author Hans Speijer
027: * @author Pierre van Rooden
028: * @version $Id: FieldDefs.java,v 1.60 2008/02/03 17:33:57 nklasens Exp $
029: * @see org.mmbase.bridge.Field
030: * @deprecated use {@link CoreField}
031: */
032: public class FieldDefs extends org.mmbase.core.CoreField {
033:
034: public final static int DBSTATE_VIRTUAL = 0;
035: public final static int DBSTATE_PERSISTENT = 2;
036: public final static int DBSTATE_SYSTEM = 3;
037:
038: public final static int DBSTATE_UNKNOWN = -1;
039:
040: public final static int ORDER_CREATE = NodeManager.ORDER_CREATE;
041: public final static int ORDER_EDIT = NodeManager.ORDER_EDIT;
042: public final static int ORDER_LIST = NodeManager.ORDER_LIST;
043: public final static int ORDER_SEARCH = NodeManager.ORDER_SEARCH;
044:
045: public FieldDefs(String name, int type, int listItemType,
046: int state, DataType<? extends Object> dataType) {
047: super (name, type, listItemType, state, dataType);
048: }
049:
050: /**
051: * Constructor for FieldDefs with partially initialized fields.
052: * @param guiName the default GUIName for a field
053: * @param guiType the GUI type (i.e. "integer' or 'field')
054: * @param searchPos position in the editor for this field when searching
055: * @param listPos position in the editor for this field when listing
056: * @param name the actual name of the field in the database
057: * @param type the basic MMBase type of the field
058: */
059: public FieldDefs(String guiName, String guiType, int searchPos,
060: int listPos, String name, int type) {
061: this (guiName, guiType, searchPos, listPos, name, type, -1,
062: STATE_PERSISTENT);
063: }
064:
065: /**
066: * Constructor for FieldDefs with partially initialized fields.
067: * @param guiName the default GUIName for a field
068: * @param guiType the GUI type (i.e. "integer' or 'field')
069: * @param searchPos position in the editor for this field when searching
070: * @param listPos position in the editor for this field when listing
071: * @param name the actual name of the field in the database
072: * @param type the basic MMBase type of the field
073: * @param guiPos position in the editor for this field when editing
074: * @param state the state of the field (persistent, virtual, etc.)
075: */
076: public FieldDefs(String guiName, String guiType, int searchPos,
077: int listPos, String name, int type, int guiPos, int state) {
078: super (name, type, TYPE_UNKNOWN, state, DataTypes
079: .getDataTypeInstance(guiType, type));
080: setGUIName(guiName);
081: setSearchPosition(searchPos);
082: setEditPosition(guiPos);
083: setListPosition(listPos);
084: }
085:
086: /**
087: * Retrieve the database name of the field.
088: * @deprecated use {@link #getName}
089: */
090: public String getDBName() {
091: return getName();
092: }
093:
094: /**
095: * Retrieves the basic MMBase type of the field.
096: *
097: * @return The type, this is one of the values defined in this class.
098: * @deprecated to access type constraints, use {@link #getDataType}
099: */
100: public int getDBType() {
101: return getType();
102: }
103:
104: /**
105: * Retrieve size of the field.
106: * This may not be specified for some field types.
107: * @deprecated Use {@link #getMaxLength}
108: */
109: public int getDBSize() {
110: return getMaxLength();
111: }
112:
113: /**
114: * Retrieve whether the field can be left blank.
115: * @deprecated use {@link #isRequired}
116: */
117: public boolean getDBNotNull() {
118: return getDataType().isRequired();
119: }
120:
121: /**
122: * Retrieve whether the field is a key and thus need be 'unique'.
123: * @deprecated use {@link #isUnique} to determine if a field is unique,
124: * use getIndexes() to return set of Index objects to which this key belongs
125: */
126: public boolean isKey() {
127: return getParent() != null
128: && getParent().getStorageConnector().isInIndex(
129: Index.MAIN, this );
130: }
131:
132: /**
133: * Temporary implementation for backwards-compatibility.
134: * I18n stuff in FieldDefs used to use String->String maps. We now have Locale->String maps
135: * available.
136: * This maps new situation to old situation.
137: */
138: protected class LocaleToStringMap extends
139: AbstractMap<String, String> {
140: private final Map<Locale, String> map;
141:
142: public LocaleToStringMap(Map<Locale, String> m) {
143: map = m;
144: }
145:
146: public Set<Entry<String, String>> entrySet() {
147: return new AbstractSet<Entry<String, String>>() {
148: public Iterator<Entry<String, String>> iterator() {
149: return new Iterator<Entry<String, String>>() {
150: private final Iterator<Entry<Locale, String>> i = map
151: .entrySet().iterator();
152:
153: public boolean hasNext() {
154: return i.hasNext();
155: }
156:
157: public void remove() {
158: throw new UnsupportedOperationException("");
159: }
160:
161: public Entry<String, String> next() {
162: final Entry<Locale, String> entry = i
163: .next();
164: return new Map.Entry<String, String>() {
165: public String getKey() {
166: return entry.getKey().getLanguage();
167: }
168:
169: public String getValue() {
170: return entry.getValue();
171: }
172:
173: public String setValue(String o) {
174: return entry.setValue(o);
175: }
176: };
177:
178: }
179: };
180: }
181:
182: public int size() {
183: return map.size();
184: }
185: };
186: }
187: }
188:
189: /**
190: * @deprecated use {@link #getGUIName(Locale locale)}
191: */
192: public String getGUIName(String lang) {
193: return getGUIName(new Locale(lang, ""));
194: }
195:
196: /**
197: * @deprecated use {@link #getGUIName()}
198: */
199: public Map<String, String> getGUINames() {
200: return new LocaleToStringMap(getLocalizedGUIName().asMap());
201: }
202:
203: /**
204: * @deprecated use {@link #getDescription()}
205: */
206: public Map<String, String> getDescriptions() {
207: return new LocaleToStringMap(getLocalizedDescription().asMap());
208: }
209:
210: /**
211: * @deprecated use {@link #getDescription(Locale locale)}
212: */
213: public String getDescription(String lang) {
214: return getDescription(new Locale(lang, ""));
215: }
216:
217: /**
218: * @deprecated use {@link #getState}
219: */
220: public int getDBState() {
221: return getState();
222: }
223:
224: /**
225: * @deprecated should not be called, name need be specified in the constructor
226: */
227: /*
228: public void setDBName(String name) {
229: key = name;
230: setLocalizedDescription(new org.mmbase.util.LocalizedString(key));
231: setLocalizedGUIName(new org.mmbase.util.LocalizedString(key));
232: }
233: */
234:
235: /**
236: * SetUI the GUI name of the field for a specified langauge.
237: * @param lang the language to set the name for
238: * @param value the value to set
239: * @deprecated to access type constraints, use {@link #getDataType}
240: */
241: public void setGUIName(String lang, String value) {
242: setGUIName(value, new Locale(lang, ""));
243: }
244:
245: /**
246: * Set the description of the field for a specified langauge.
247: * @param lang the language to set the description for
248: * @param value the value to set
249: * @deprecated use {@link #getLocalizedDescription}
250: */
251: public void setDescription(String lang, String value) {
252: setDescription(value, new Locale(lang, ""));
253: }
254:
255: /**
256: * Set size of the field.
257: * @param value the value to set
258: * @deprecated use {@link CoreField#setMaxLength}
259: */
260: public void setDBSize(int value) {
261: setMaxLength(value);
262: }
263:
264: /**
265: * Set the position of the field in the database table.
266: * @param value the value to set
267: * @deprecated use {@link #setStoragePosition}
268: */
269: public void setDBPos(int value) {
270: setStoragePosition(value);
271: }
272:
273: /**
274: * @deprecated use {@link #getStoragePosition}
275: */
276: public int getDBPos() {
277: return getStoragePosition();
278: }
279:
280: /**
281: * Set the position of the field when listing.
282: * A value of -1 indicates teh field is unavailable in a list.
283: * @param value the value to set
284: * @deprecated use {@link #setListPosition}
285: */
286: public void setGUIList(int value) {
287: setListPosition(value);
288: }
289:
290: /**
291: * @deprecated use {@link #getListPosition}
292: */
293: public int getGUIList() {
294: return getListPosition();
295: }
296:
297: /**
298: * Set the position of the field when editing.
299: * A value of -1 indicates the field cannot be edited.
300: * @param value the value to set
301: * @deprecated use {@link #setEditPosition}
302: */
303: public void setGUIPos(int value) {
304: setEditPosition(value);
305: }
306:
307: /**
308: * @deprecated use {@link #getEditPosition}
309: */
310: public int getGUIPos() {
311: return getEditPosition();
312: }
313:
314: /**
315: * Set the position of the field when searching.
316: * A value of -1 indicates teh field is unavailable during search.
317: * @param value the value to set
318: * @deprecated use {@link #setSearchPosition}
319: */
320: public void setGUISearch(int value) {
321: setSearchPosition(value);
322: }
323:
324: /**
325: * @deprecated use {@link #getSearchPosition}
326: */
327: public int getGUISearch() {
328: return getSearchPosition();
329: }
330:
331: /**
332: * Set the basic MMBase state of the field, using the state description
333: * @param value the name of the state
334: * @deprecated use {@link #setState}
335: */
336: public void setDBState(String value) {
337: setState(Fields.getState(value));
338: }
339:
340: /**
341: * @deprecated use {@link #getState}
342: */
343: public void setDBState(int i) {
344: setState(i);
345: }
346:
347: /**
348: * Set whether the field is a key and thus needs to be 'unique'.
349: * @param value the value to set
350: * @deprecated use {@link CoreField#setUnique} to make a field unique.
351: */
352: //use {@link org.mmbase.module.core.MMTable#addToIndex} to make the field part of an index
353: public void setDBKey(boolean value) {
354: if (getParent() != null) {
355: getParent().getStorageConnector().addToIndex(Index.MAIN,
356: this );
357: }
358: }
359:
360: /**
361: * Set whether the field can be left blank.
362: * @param value the value to set
363: * @deprecated to access type constraints, use {@link #getDataType}
364: */
365: public void setDBNotNull(boolean value) {
366: getDataType().setRequired(value);
367: }
368:
369: /**
370: * Sorts a list with FieldDefs objects, using the default order (ORDER_CREATE)
371: * @param fielddefs the list to sort
372: * @deprecated use Collections.sort
373: */
374: public static void sort(List<FieldDefs> fielddefs) {
375: Collections.sort(fielddefs);
376: }
377:
378: /**
379: * Sorts a list with FieldDefs objects, using the specified order
380: * @param fielddefs the list to sort
381: * @param order one of ORDER_CREATE, ORDER_EDIT, ORDER_LIST,ORDER_SEARCH
382: * @deprecated use {@link Fields#sort}
383: */
384: public static void sort(List<CoreField> fielddefs, int order) {
385: Fields.sort(fielddefs, order);
386: }
387:
388: }
|