/*
* Copyright 2012 Adaptrex, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adaptrex.core.persistence.api;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import com.adaptrex.core.ext.ExtTypeFormatter;
import com.adaptrex.core.ext.FieldDefinition;
import com.adaptrex.core.security.SecureEntity;
import com.adaptrex.core.security.SecureField;
public abstract class AdaptrexFieldType {
private static Logger log = Logger.getLogger(AdaptrexFieldType.class);
private String name;
private Class<?> fieldType;
private SecureField secureField;
private FieldDefinition fieldDefinition;
protected Method getter;
protected Method setter;
public AdaptrexFieldType(String name, Class<?> fieldType, SecureEntity secureEntity) {
this.name = name;
this.fieldType = fieldType;
if (secureEntity != null) {
this.secureField = secureEntity.getField(name);
}
this.fieldDefinition =
new FieldDefinition(name, ExtTypeFormatter.getType(fieldType));
log.info("Initializing: " + name + " (" +
this.fieldType.getSimpleName() + ")"
);
}
public String getName() {
return this.name;
}
public Class<?> getFieldType() {
return this.fieldType;
}
public FieldDefinition getFieldDefinition() {
return this.fieldDefinition;
}
public void setValueFor(Object entity, Object value) throws Exception {
this.setter.invoke(entity, value);
}
public Object getValueFrom(Object entity) throws Exception {
return this.getter.invoke(entity);
}
public SecureField getSecureField() {
return secureField;
}
}