📄 attributeimpl.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.entities.dynamictype.internal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import org.rapla.components.util.SerializableDateTimeFormat;
import org.rapla.components.util.Tools;
import org.rapla.entities.Category;
import org.rapla.entities.EntityNotFoundException;
import org.rapla.entities.IllegalAnnotationException;
import org.rapla.entities.MultiLanguageName;
import org.rapla.entities.RaplaType;
import org.rapla.entities.dynamictype.Attribute;
import org.rapla.entities.dynamictype.AttributeType;
import org.rapla.entities.dynamictype.ConstraintIds;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.entities.internal.CategoryImpl;
import org.rapla.entities.storage.EntityResolver;
import org.rapla.entities.storage.Mementable;
import org.rapla.entities.storage.RefEntity;
import org.rapla.entities.storage.internal.SimpleEntity;
import org.rapla.storage.LocalCache;
public class AttributeImpl extends SimpleEntity implements Attribute,Mementable,java.io.Serializable {
// Don't forget to increase the serialVersionUID when you change the fields
private static final long serialVersionUID = 2;
private MultiLanguageName name = new MultiLanguageName();
private AttributeType type;
private String key;
private boolean bOptional = false;
private HashMap annotations = new HashMap();
public final static AttributeType DEFAULT_TYPE = AttributeType.STRING;
public AttributeImpl() {
this.type = DEFAULT_TYPE;
}
public AttributeImpl(AttributeType type) {
setType(type);
}
void setParent(DynamicType parent) {
getReferenceHandler().put("parent",parent);
}
public DynamicType getDynamicType() {
return (DynamicType)getReferenceHandler().get("parent");
}
final public RaplaType getRaplaType() {return TYPE;}
public AttributeType getType() {
return type;
}
public void setType(AttributeType type) {
this.type = type;
}
public MultiLanguageName getName() {
return name;
}
public void setReadOnly(boolean enable) {
super.setReadOnly( enable );
name.setReadOnly( enable );
}
public String getName(Locale locale) {
return name.getName(locale.getLanguage());
}
public String getKey() {
return key;
}
public void setConstraint(String key,Object constraint) {
checkWritable();
if ( getConstraintClass( key ) == Category.class ) {
getReferenceHandler().put("constraint." + key,(Category)constraint);
}
}
public Object getConstraint(String key) {
if ( getConstraintClass( key ) == Category.class ) {
return getReferenceHandler().get("constraint." + key);
}
return null;
}
public Class getConstraintClass(String key) {
if (key.equals(ConstraintIds.KEY_ROOT_CATEGORY)) {
return Category.class;
}
return String.class;
}
public String[] getConstraintKeys() {
if (type.equals( AttributeType.CATEGORY)) {
return new String[] {ConstraintIds.KEY_ROOT_CATEGORY};
} else {
return new String[0];
}
}
public void setKey(String key) {
checkWritable();
this.key = key;
}
public boolean isValid(Object obj) {
return true;
}
public boolean isOptional() {
return bOptional;
}
public void setOptional(boolean bOptional) {
checkWritable();
this.bOptional = bOptional;
}
public Object defaultValue() {
if (type.equals( AttributeType.BOOLEAN)) {
return Boolean.FALSE;
}
return null;
}
public boolean needsChange(Object value) {
if (value == null)
return false;
if (type.equals( AttributeType.STRING )) {
return !(value instanceof String);
}
if (type.equals( AttributeType.INT )) {
return !(value instanceof Long);
}
if (type.equals( AttributeType.DATE )) {
return !(value instanceof Date);
}
if (type.equals( AttributeType.BOOLEAN )) {
return !(value instanceof Boolean);
}
if (type.equals( AttributeType.CATEGORY )) {
if (!(value instanceof Category))
return true;
Category temp = (Category) value;
// look if the attribute category is a ancestor of the value category
Category rootCategory = (Category) getConstraint(ConstraintIds.KEY_ROOT_CATEGORY);
boolean change = ( rootCategory == null || !rootCategory.isAncestorOf( temp ));
return change;
}
return false;
}
public Object convertValue(Object value) {
if (type.equals( AttributeType.STRING )) {
if (value == null)
return null;
return value.toString();
}
if (type.equals( AttributeType.DATE )) {
if (value == null)
return null;
return new Date();
}
if (type.equals( AttributeType.INT )) {
if (value == null)
return null;
if (value instanceof Boolean)
return ((Boolean) value).booleanValue() ? new Long(1) : new Long(0);
String str = value.toString().trim().toLowerCase();
try {
return new Long(str);
} catch (NumberFormatException ex) {
return null;
}
}
if (type.equals( AttributeType.BOOLEAN )) {
if (value == null)
return Boolean.FALSE;
String str = value.toString().trim().toLowerCase();
if (str.equals("0") || str.equals("false"))
return Boolean.FALSE;
else
return Boolean.TRUE;
}
if (type.equals( AttributeType.CATEGORY )) {
if (value == null)
return null;
if (value instanceof Category) {
Category temp = (Category) value;
Category rootCategory = (Category) getConstraint(ConstraintIds.KEY_ROOT_CATEGORY);
if ( temp != null
&& rootCategory != null
&& rootCategory.isAncestorOf( temp )
) {
return value;
}
}
}
return null;
}
public String getAnnotation(String key) {
return (String) annotations.get(key);
}
public String getAnnotation(String key, String defaultValue) {
String annotation = getAnnotation( key );
return annotation != null ? annotation : defaultValue;
}
public void setAnnotation(String key,String annotation) throws IllegalAnnotationException {
checkWritable();
if (annotation == null) {
annotations.remove(key);
return;
}
annotations.put(key,annotation);
}
public String[] getAnnotationKeys() {
return (String[]) annotations.keySet().toArray(Tools.EMPTY_STRING_ARRAY);
}
static private void copy(AttributeImpl source,AttributeImpl dest) {
dest.name = (MultiLanguageName) source.name.clone();
dest.annotations = (HashMap) source.annotations.clone();
dest.type = source.getType();
dest.setKey(source.getKey());
dest.setOptional(source.isOptional());
String[] constraintKeys = source.getConstraintKeys();
for ( int i = 0;i < constraintKeys.length; i++) {
String key = constraintKeys[ i ];
dest.setConstraint( key, source.getConstraint(key));
}
}
public void copy(Object obj) {
super.copy((AttributeImpl)obj);
copy((AttributeImpl) obj,this);
}
public Object deepClone() {
AttributeImpl clone = new AttributeImpl();
super.deepClone(clone);
copy(this,clone);
return clone;
}
public Object clone() {
AttributeImpl clone = new AttributeImpl();
super.clone(clone);
copy(this,clone);
return clone;
}
public String toString() {
MultiLanguageName name = getName();
if (name != null) {
return name.toString()+ " ID='" + getId() + "'";
} else {
return getKey() + " " + getId();
}
}
/** @param idResolver if this is set the category will be resolved over the id value*/
static public Object parseAttributeValue(Attribute attribute,String text, EntityResolver idResolver) throws ParseException {
AttributeType type = attribute.getType();
if (type.equals(AttributeType.BOOLEAN)) {
return text.trim().equals("true") ?
Boolean.TRUE : Boolean.FALSE;
}
if (type.equals( AttributeType.DATE )) {
return new SerializableDateTimeFormat().parseDate( text.trim(), false);
}
if (type.equals( AttributeType.INT)) {
try {
return new Long( text.trim() );
} catch (NumberFormatException ex) {
throw new ParseException( ex.getMessage(), 0);
}
}
if (type.equals( AttributeType.STRING )) {
return text.trim();
}
if (type.equals( AttributeType.CATEGORY )) {
String path = text.trim();
if (path.length() == 0) {
return null;
}
if (idResolver != null) {
try {
Object id = LocalCache.getId( Category.TYPE, path);
return (Category) idResolver.resolve( id );
} catch (EntityNotFoundException ex) {
throw new ParseException(ex.getMessage(), 0);
}
} else {
CategoryImpl rootCategory = (CategoryImpl)attribute.getConstraint(ConstraintIds.KEY_ROOT_CATEGORY);
if (rootCategory == null) {
//System.out.println( attribute.getConstraintKeys());
throw new ParseException("Can't find " + ConstraintIds.KEY_ROOT_CATEGORY + " for attribute " + attribute, 0);
}
try {
return rootCategory.getCategoryFromPath(path);
} catch (Exception ex) {
throw new ParseException(ex.getMessage(), 0);
}
}
}
throw new ParseException("Unknown attribute type: " + type , 0);
}
public static String attributeValueToString( Attribute attribute, Object value, boolean idOnly) throws EntityNotFoundException {
AttributeType type = attribute.getType();
if (type.equals( AttributeType.CATEGORY ))
{
CategoryImpl rootCategory = (CategoryImpl) attribute.getConstraint(ConstraintIds.KEY_ROOT_CATEGORY);
if ( idOnly) {
return ((RefEntity)value).getId().toString();
} else {
return rootCategory.getPathForCategory((Category)value) ;
}
}
else if (type.equals( AttributeType.DATE ))
{
return new SerializableDateTimeFormat().formatDate( (Date)value ) ;
}
else
{
return value.toString() ;
}
}
static public class IntStrategy {
String[] constraintKeys = new String[] {"min","max"};
public String[] getConstraintKeys() {
return constraintKeys;
}
public boolean needsChange(Object value) {
return !(value instanceof Long);
}
public Object convertValue(Object value) {
if (value == null)
return null;
if (value instanceof Boolean)
return ((Boolean) value).booleanValue() ? new Long(1) : new Long(0);
String str = value.toString().trim().toLowerCase();
try {
return new Long(str);
} catch (NumberFormatException ex) {
return null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -