/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package reportgen.cores.ejb;
import reportgen.cores.ejb.annotations.DefineQueryProperty;
import reportgen.cores.ejb.annotations.DefineQueryEntity;
import javax.persistence.Entity;
import reportgen.prototype.entity.QElement;
/**
* Основной объект отчета
* имеет класс, описание, название, тег и идентификатор
* Преобразует класс целых чисел в Long, не целых - в Double
* @author axe
*/
abstract class QueryObject implements QElement {
/**
* Тип объекта
*/
private Class cls;
/**
* Название объекта
*/
private String title;
/**
* Описание объекта
*/
private String desc;
/**
* идентификатор объекта
*/
private String identification;
/**
* Создает объект. Название и описание берутся из типа.
* @param tagName тег объекта
* @param cls тип объекта
* @param identification идентификатор объекта
*/
public QueryObject(Class cls, String identification) {
this.cls = convertToGeneric(cls);
this.identification = identification;
reReadClassInfo();
}
/**
* Создает объект
* @param tagName тег объекта
* @param cls тип объекта
* @param identification идентификатор объекта
* @param title название
* @param desc описание
*/
protected QueryObject(Class cls, String identification, String title, String desc) {
this.cls = convertToGeneric(cls);
this.title = title;
this.desc = desc;
this.identification = identification;
}
/**
* Читает название и описание объекта из типа
*/
public void reReadClassInfo() {
DefineQueryProperty gen = (DefineQueryProperty) cls.getAnnotation(DefineQueryProperty.class);
if(gen != null) {
title = gen.title();
desc = gen.desc();
} else {
DefineQueryEntity gene = (DefineQueryEntity) cls.getAnnotation(DefineQueryEntity.class);
if(gene != null) {
title = gene.title();
desc = gene.desc();
} else {
assert cls.isAnnotationPresent(Entity.class);
title = cls.getSimpleName();
}
}
}
@Override
public String toString() {
return getTitle();
}
@Override
public Class getCls() {
return cls;
}
@Override
public String getDescription() {
return desc;
}
@Override
public String getIdentification() {
return identification;
}
@Override
public String getTitle() {
return title;
}
/**
* Конвертит примитивные классы в объектные
* @param c
* @return
*/
public static Class convertToGeneric(Class c) {
if(c == long.class
|| c == int.class
|| c == Integer.class
|| c == short.class
|| c == Short.class
|| c == byte.class
|| c == Byte.class) {
return Long.class;
} else if(c == boolean.class) {
return Boolean.class;
} else if(c == double.class
|| c == float.class
|| c == Float.class) {
return Double.class;
} else if(c == char.class) {
return Character.class;
}
return c;
}
/**
* объект является тем же если все совпадает
* @param obj
* @return
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final QueryObject other = (QueryObject) obj;
if (this.cls != other.cls && (this.cls == null || !this.cls.equals(other.cls))) {
return false;
}
if (this.title != other.title && (this.title == null || !this.title.equals(other.title))) {
return false;
}
if (this.desc != other.desc && (this.desc == null || !this.desc.equals(other.desc))) {
return false;
}
if (this.identification != other.identification && (this.identification == null || !this.identification.equals(other.identification))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 97 * hash + (this.cls != null ? this.cls.hashCode() : 0);
hash = 97 * hash + (this.title != null ? this.title.hashCode() : 0);
hash = 97 * hash + (this.desc != null ? this.desc.hashCode() : 0);
hash = 97 * hash + (this.identification != null ? this.identification.hashCode() : 0);
return hash;
}
}