package com.javaeye.jert.domain;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.javaeye.core.domain.Entity;
import com.javaeye.jert.domain.query.Query;
import com.javaeye.jert.domain.query.QueryBuilder;
import com.javaeye.jert.domain.query.QueryParameter;
/**
* @author Quake Wang
* @since 2004-12-22
* @version $Revision: 1.10 $
*/
public class ReportDefinition extends Entity {
private Database database;
private String name;
private String sql;
private String queryType;
private List parameterDefinitions = new ArrayList();
private List reportPermissionGroups = new ArrayList();
private List drillDownColumns=new ArrayList();
private List associatedDrillDownColumns=new ArrayList();
public ReportDefinition() {
}
/**
* we may use this to make a ReportDefinition copy, and export it
*/
public ReportDefinition(ReportDefinition rd) {
this.name = rd.getName();
this.sql = rd.getSql();
this.queryType = rd.getQueryType();
List pds = rd.getParameterDefinitions();
for (Iterator iter = pds.iterator(); iter.hasNext();) {
this.addParameterDefinition(new ParameterDefinition((ParameterDefinition) iter.next()));
}
}
public Query getQuery() {
return QueryBuilder.getQuery(sql, queryType);
}
public QueryParameter[] getDefaultQueryParameters() {
QueryParameter[] result = new QueryParameter[getParameterDefinitions().size()];
for (int i = 0; i < getParameterDefinitions().size(); i++) {
result[i] = new QueryParameter((ParameterDefinition) getParameterDefinitions().get(i));
}
return result;
}
public void addParameterDefinition(ParameterDefinition parameterDefinition) {
parameterDefinition.setReportDefinition(this);
parameterDefinitions.add(parameterDefinition);
}
public void removeParameterDefinition(ParameterDefinition parameterDefinition) {
parameterDefinition.setReportDefinition(null);
parameterDefinitions.remove(parameterDefinition);
}
public void addReportPermissionGroup(ReportPermissionGroup reportPermissionGroup) {
reportPermissionGroup.setReportDefinition(this);
reportPermissionGroups.add(reportPermissionGroup);
}
public void removeReportPermissionGroup(ReportPermissionGroup reportPermissionGroup) {
reportPermissionGroup.setReportDefinition(null);
reportPermissionGroups.remove(reportPermissionGroup);
}
public void setupDefaultParameterDefinition() {
List toBeRemoved = calculateToBeRemoved();
List toBeAdded = calculateToBeAdded();
for (Iterator iter = toBeAdded.iterator(); iter.hasNext();) {
addParameterDefinition((ParameterDefinition) iter.next());
}
for (Iterator iter = toBeRemoved.iterator(); iter.hasNext();) {
removeParameterDefinition((ParameterDefinition) iter.next());
}
}
private List calculateToBeAdded() {
List toBeAdded = new ArrayList();
List names = getQuery().getParameterNames();
for (Iterator iter = names.iterator(); iter.hasNext();) {
String name = (String) iter.next();
boolean found = false;
for (Iterator iterator = parameterDefinitions.iterator(); iterator.hasNext();) {
ParameterDefinition p = (ParameterDefinition) iterator.next();
if (p.getName().equalsIgnoreCase(name)) {
found = true;
break;
}
}
if (!found)
toBeAdded.add(new ParameterDefinition(name));
}
return toBeAdded;
}
private List calculateToBeRemoved() {
List toBeRemoved = new ArrayList();
List names = getQuery().getParameterNames();
for (Iterator iter = parameterDefinitions.iterator(); iter.hasNext();) {
ParameterDefinition p = (ParameterDefinition) iter.next();
boolean found = false;
for (Iterator iterator = names.iterator(); iterator.hasNext();) {
String name = (String) iterator.next();
if (p.getName().equalsIgnoreCase(name)) {
found = true;
break;
}
}
if (!found)
toBeRemoved.add(p);
}
return toBeRemoved;
}
public Database getDatabase() {
return database;
}
public String getName() {
return name;
}
public List getParameterDefinitions() {
return parameterDefinitions;
}
public String getQueryType() {
return queryType;
}
public String getSql() {
return sql;
}
public void setDatabase(Database database) {
this.database = database;
}
public void setName(String name) {
this.name = name;
}
public void setParameterDefinitions(List parameterDefinitions) {
this.parameterDefinitions = parameterDefinitions;
}
public void setQueryType(String queryType) {
this.queryType = queryType;
}
public void setSql(String sql) {
this.sql = sql;
}
public List getReportPermissionGroups() {
return reportPermissionGroups;
}
public void setReportPermissionGroups(List reportPermissionGroups) {
this.reportPermissionGroups = reportPermissionGroups;
}
public List getDrillDownColumns() {
return drillDownColumns;
}
public void setDrillDownColumns(List drillDownColumns) {
this.drillDownColumns = drillDownColumns;
}
public void addDrillDownColumn(DrillDownColumn ddc){
ddc.setReportDefinition(this);
drillDownColumns.add(ddc);
}
public void removeDrillDownColumn(DrillDownColumn ddc){
ddc.setReportDefinition(null);
drillDownColumns.remove(ddc);
}
public List getAssociatedDrillDownColumns() {
return associatedDrillDownColumns;
}
public void setAssociatedDrillDownColumns(List associatedDrillDownColumns) {
this.associatedDrillDownColumns = associatedDrillDownColumns;
}
public void addAssociatedDrillDownColumn(DrillDownColumn ddc){
ddc.setToReportDefinition(this);
associatedDrillDownColumns.add(ddc);
}
public void removeAssociatedDrillDownColumn(DrillDownColumn ddc){
ddc.setToReportDefinition(null);
associatedDrillDownColumns.remove(ddc);
}
}