/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.report;
import framework.beans.report.ReportBeanRemote;
import beans.report.selectors.baseSelectors.AbstractReportTable;
import beans.report.selectors.ReportTableFactory;
import framework.beans.FacadeBean;
import beans.report.selectors.baseSelectors.AbstractTablesSelector;
import beans.report.selectors.data.DateRange;
import framework.beans.security.BeanRights;
import framework.beans.EntityDetails;
import framework.beans.report.ReportDefinition;
import framework.generic.ClipsServerException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Stateful;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import org.jdom.Element;
import beans.UserRightsSet;import beans.directory.lpu.entity.Lpu;
import beans.user.collaborator.entities.Collaborator;
import framework.audit.AuditDoc;
import framework.security.RightChecker;
import java.util.List;
/**
*
* @author finder
*/
@Stateful(mappedName="clips-beans/DiseaseReportBean")
public class DiseaseReportBean extends FacadeBean<DiseaseReport> implements DiseaseReportBeanRemote {
public static final int COMMAND_READ_REPORT = 4;
public DiseaseReportBean() {
super(DiseaseReport.class);
}
@Override
protected void initBeanRights() {
int [] r = new int[5];
r[COMMAND_READ] = RightPresence(UserRightsSet.READ_REPORT_STRUCTURE.id);
int cmr = RightPresence(UserRightsSet.WRITE_REPORT_STRUCTURE.id);
r[COMMAND_CREATE] = cmr;
r[COMMAND_MODIFY] = cmr;
r[COMMAND_REMOVE] = cmr;
r[COMMAND_READ_REPORT] = RightPresence(UserRightsSet.EXECUTE_REPORT.id);
rights = new BeanRights(r);
}
@Override
protected void onUpdate(DiseaseReport entity, EntityDetails details,
AuditDoc auditDoc, List<AuditDoc> auditDocList) throws ClipsServerException {
DiseaseReportDetals det = (DiseaseReportDetals)details;
entity.setReportName(det.name);
entity.setReportItems(det.itemsXML);
entity.setReportType(det.type);
entity.setResolutionDateNumber(det.resolutionDateNumber);
entity.setFormNumber(det.formNumber);
entity.setOkudFormCode(det.okudFormCode);
entity.setTargetDeaprtaments(det.targetDeaprtaments);
entity.setTargetDates(det.targetDates);
if (det.reportId != 0) {
entity.setReport(findEntity(ReportDefinition.class, det.reportId));
}
else {
entity.setReport(null);
}
}
AbstractTablesSelector<?> createTableSelector(AbstractReportTable table, Lpu lpu, DateRange range, int tableId) throws ClipsServerException{
return new ReportTableFactory().createTableSelector(manager, lpu, table, tableId, range);
}
JRDataSource getMainDataSourse(int year) throws ClipsServerException{
Collaborator cool = findEntity(Collaborator.class, getCollaboratorId());
Lpu lpu = cool.getLpu();
DiseaseReport entity = getExistentEntity();
ArrayList<Map<String, Object>> mainData = new ArrayList<Map<String, Object>>();
Element xml = entity.getDetails((RightChecker) this).getXml();
ReportTableFactory factory = new ReportTableFactory();
ArrayList<AbstractReportTable<?>> list = factory.parseXml(xml);
DateRange range = new DateRange(year);
for (int i = 0; i < list.size(); i++) {
AbstractReportTable table = list.get(i);
AbstractTablesSelector<?> selector = createTableSelector(table, lpu, range, i);
Collection<Map<String, Object>> newData = selector.getDataList(year);
mainData.addAll(newData);
}
JRDataSource mainDataSourse = new JRMapCollectionDataSource(mainData);
return mainDataSourse;
}
@Override
public JasperPrint getReport(int year) throws JRException, ClipsServerException {
try{
checkCommandAccessibility(COMMAND_READ_REPORT);
DiseaseReport entity = getExistentEntity();
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("deserase_group_name", entity.getReportName());
parameters.put("id_formcode", entity.getOkudFormCode());
parameters.put("form_number_name", entity.getFormNumber());
parameters.put("form_order_date", entity.getResolutionDateNumber());
parameters.put("target_department_list", entity.getTargetDeaprtaments());
parameters.put("target_times", entity.getTargetDates());
parameters.put("year_as_num", year);
JRDataSource mainDataSourse = getMainDataSourse(year);
parameters.put(JRParameter.REPORT_DATA_SOURCE, mainDataSourse);
return buildReport(parameters);
}
catch (RuntimeException ex){
throw new ClipsServerException("Ошибка при получении отчета", ex);
}
}
private JasperPrint buildReport(Map<String, Object> parameters) throws ClipsServerException, JRException{
ReportBeanRemote bean = getBean(ReportBean.class);
DiseaseReport entity = getExistentEntity();
bean.initByID(entity.getReport().getId(), getSessionId());
return bean.getReport(parameters);
}
}