Implementace uložiště podepsaných dokumentů. Při prvním podpisu se uloží podepsané PDF a vytvoří se záznam v agendě podepsaných dokumentů.

closes #243
This commit is contained in:
2015-07-08 12:55:41 +02:00
parent 6db416dd04
commit cccd8177fa
19 changed files with 337 additions and 71 deletions
@@ -0,0 +1,25 @@
package info.bukova.isspst;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
/**
* @author Pepa Rokos
*/
public class SpringUtils {
private static WebApplicationContext webCtx(ServletContext sc) {
return WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
}
public static Object getBean(String name, ServletContext sc) {
return webCtx(sc).getBean(name);
}
public static <T> T getBean(Class<T> clazz, ServletContext sc) {
return webCtx(sc).getBean(clazz);
}
}
@@ -3,19 +3,18 @@ package info.bukova.isspst.data;
import info.bukova.isspst.Constants;
import info.bukova.isspst.Module;
import info.bukova.isspst.StringUtils;
import java.util.Date;
import java.util.List;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.search.annotations.IndexedEmbedded;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.search.annotations.IndexedEmbedded;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "SIGNED_DOCUMENTS")
@@ -105,6 +104,10 @@ public class SignedDocument extends BaseData {
}
public void addItem(SignedDocumentItem item) {
if (items == null) {
items = new ArrayList<SignedDocumentItem>();
}
item.setSignedDocument(this);
this.items.add(item);
}
@@ -22,7 +22,7 @@ public class SignedDocumentItem {
private int id;
@Column(name = "REPORT_ID")
private int reportId;
private long reportId;
@Column(name = "REPORT_NAME", length = Constants.LEN_TEXT)
private String reportName;
@@ -42,11 +42,11 @@ public class SignedDocumentItem {
this.id = id;
}
public int getReportId() {
public long getReportId() {
return reportId;
}
public void setReportId(int reportId) {
public void setReportId(long reportId) {
this.reportId = reportId;
}
@@ -74,7 +74,7 @@ public class ParamFiller {
definition.setParam("P_PREV_APPROVER_SIGNATURE", storage.serverPath(prevApproverSettings.getSignatureFile()));
}
AuthItem lastAuth = tb.getApproval().getAuthorization().get(tb.getRequirement().getAuthorization().size() - 1);
AuthItem lastAuth = tb.getApproval().getAuthorization().get(tb.getApproval().getAuthorization().size() - 1);
definition.setParam("P_APPROVE_DATE", lastAuth.getAuthDate());
User u = lastAuth.getApprover();
@@ -1,18 +1,21 @@
package info.bukova.isspst.reporting;
import java.io.File;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.bukova.isspst.SpringUtils;
import info.bukova.isspst.data.DataModel;
import info.bukova.isspst.data.SignedDocumentItem;
import info.bukova.isspst.services.signeddocs.SignedDocumentService;
import info.bukova.isspst.storage.ReportFileStorage;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.util.JRProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContext;
import java.io.File;
@SuppressWarnings("deprecation")
public class PredefinedGenerator implements Generator {
@@ -20,7 +23,7 @@ public class PredefinedGenerator implements Generator {
private ReportDefinition definition;
private ServletContext ctx;
private final static Logger logger = LoggerFactory.getLogger(PredefinedGenerator.class);
public PredefinedGenerator(ReportDefinition definition, ServletContext ctx) {
this.definition = definition;
this.ctx = ctx;
@@ -29,7 +32,12 @@ public class PredefinedGenerator implements Generator {
@Override
public byte[] generate() {
byte[] bytes = null;
SignedDocumentItem signedItem = getSignedDocument();
if (signedItem != null) {
return fromStorage(signedItem);
}
try {
JasperReport report = (JasperReport) JRLoader.loadObject(getReportFile());
JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "Cp1250");
@@ -43,6 +51,18 @@ public class PredefinedGenerator implements Generator {
return bytes;
}
private byte[] fromStorage(SignedDocumentItem sdItem) {
ReportFileStorage reportStorage = SpringUtils.getBean(ReportFileStorage.class, ctx);
return reportStorage.fileData(sdItem);
}
private SignedDocumentItem getSignedDocument() {
SignedDocumentService sdService = SpringUtils.getBean(SignedDocumentService.class, ctx);
SignedDocumentItem sdItem = sdService.getItem((DataModel) definition.getDataSet().get(0), definition.getReport().getReportId());
return sdItem;
}
protected File getReportFile() {
return new File(ctx.getRealPath("WEB-INF/reports") + "/" + definition.getReport().getJasperFile() + ".jasper");
@@ -1,17 +1,17 @@
package info.bukova.isspst.reporting;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import info.bukova.isspst.storage.ReportFileStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
@Controller
public class ReportController {
@@ -21,6 +21,8 @@ public class ReportController {
private GeneratorFactory factory;
@Autowired
private ParamFiller paramFiller;
@Autowired
private ReportFileStorage reportFileStorage;
private static final String ERROR_MESSAGE = "<html><body><b>Generator returned no data!</b><br/>%s</body></html>";
private final static Logger logger = LoggerFactory.getLogger(ReportController.class);
@@ -46,13 +48,20 @@ public class ReportController {
try {
os = response.getOutputStream();
if (reportDefinition.getReport() == null || reportDefinition.getDataSet() == null) {
if ((reportDefinition.getReport() == null || reportDefinition.getDataSet() == null) && reportDefinition.getSignedDocItem() == null) {
throw new ReportException("Definition is null");
}
paramFiller.fill();
Generator gen = factory.createGenerator(reportDefinition);
byte[] data = gen.generate();
byte[] data;
if (reportDefinition.getSignedDocItem() == null) {
paramFiller.fill();
Generator gen = factory.createGenerator(reportDefinition);
data = gen.generate();
} else {
data = reportFileStorage.fileData(reportDefinition.getSignedDocItem());
}
response.setContentType(contentType);
response.setContentLength(data.length);
@@ -1,5 +1,6 @@
package info.bukova.isspst.reporting;
import info.bukova.isspst.data.SignedDocumentItem;
import info.bukova.isspst.services.Service;
import java.io.Serializable;
@@ -20,6 +21,7 @@ public class ReportDefinition implements Serializable {
private List<String> fieldsToPrint;
private String reportTitle;
private Service<Object> service;
private SignedDocumentItem signedDocItem;
public ReportDefinition() {
params = new HashMap<String, Object>();
@@ -92,6 +94,7 @@ public class ReportDefinition implements Serializable {
reportTitle = "";
service = null;
params.clear();
signedDocItem = null;
}
public String getReportTitle() {
@@ -110,4 +113,11 @@ public class ReportDefinition implements Serializable {
this.service = service;
}
public SignedDocumentItem getSignedDocItem() {
return signedDocItem;
}
public void setSignedDocItem(SignedDocumentItem signedDocItem) {
this.signedDocItem = signedDocItem;
}
}
@@ -10,6 +10,8 @@ import info.bukova.isspst.data.RequirementBase;
import info.bukova.isspst.data.RequirementState;
import info.bukova.isspst.data.Role;
import info.bukova.isspst.data.SettingsData;
import info.bukova.isspst.data.SignedDocument;
import info.bukova.isspst.data.SignedDocumentItem;
import info.bukova.isspst.data.User;
import info.bukova.isspst.data.Workflow;
import info.bukova.isspst.data.Workgroup;
@@ -25,10 +27,12 @@ import info.bukova.isspst.services.AbstractOwnedService;
import info.bukova.isspst.services.LazyLoader;
import info.bukova.isspst.services.Service;
import info.bukova.isspst.services.settings.GlobalSettingsService;
import info.bukova.isspst.services.signeddocs.SignedDocumentService;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.services.workgroups.WorkgroupService;
import info.bukova.isspst.signapi.SignData;
import info.bukova.isspst.storage.FileStorage;
import info.bukova.isspst.storage.ReportFileStorage;
import org.hibernate.LazyInitializationException;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
@@ -73,6 +77,10 @@ public abstract class RequirementBaseServiceImpl<T extends RequirementBase> exte
private ServletContext servletContext;
@Autowired
private FileStorage storage;
@Autowired
private SignedDocumentService signedDocumentService;
@Autowired
private ReportFileStorage signedDocStorage;
@Override
@Transactional
@@ -292,6 +300,7 @@ public abstract class RequirementBaseServiceImpl<T extends RequirementBase> exte
}
}
saveSignedDoc(e);
postApprove(e);
}
@@ -333,6 +342,36 @@ public abstract class RequirementBaseServiceImpl<T extends RequirementBase> exte
}
protected void saveSignedDoc(T entity) {
/*
if (!entity.equals(reportDefinition.getDataSet().get(0))) {
throw new ApproveException();
}*/
DataModel reportEntity = (DataModel) reportDefinition.getDataSet().get(0);
SignedDocumentItem signedItem = signedDocumentService.getItem(entity, reportDefinition.getReport().getReportId());
SignData data = (SignData) sessionData.getProperty(Constants.KEY_SIGN_DATA);
if (signedItem == null) {
data = (SignData) sessionData.getProperty(Constants.KEY_SIGN_DATA);
SignedDocument signDoc = new SignedDocument();
signDoc.setDescription(entity.getDescription());
signDoc.setNumser(entity.getNumser());
signDoc.setRecordId(reportEntity.getId());
signDoc.setModuleName(ModuleUtils.getModule(reportEntity, servletContext).getId());
SignedDocumentItem item = new SignedDocumentItem();
item.setReportId(reportDefinition.getReport().getReportId());
item.setReportName(reportDefinition.getReport().getName());
signDoc.addItem(item);
signedDocStorage.saveFile(data.getPdfData(), item);
signedDocumentService.addFromApprove(signDoc);
} else {
signedDocStorage.saveFile(data.getPdfData(), signedItem);
}
}
@Override
@Transactional
public boolean canApprove(T entity) {
@@ -464,6 +503,7 @@ public abstract class RequirementBaseServiceImpl<T extends RequirementBase> exte
data.setDescription(entity.getDescription());
data.setNumser(entity.getNumser());
data.setSignDate(approveDate);
data.setSigned(signedDocumentService.getItem(entityForReport(entity), report.getReportId()) != null);
if (entity.getState() == RequirementState.NEW) {
data.setAreaId(1);
@@ -1,8 +1,14 @@
package info.bukova.isspst.services.signeddocs;
import info.bukova.isspst.data.DataModel;
import info.bukova.isspst.data.SignedDocument;
import info.bukova.isspst.data.SignedDocumentItem;
import info.bukova.isspst.services.Service;
public interface SignedDocumentService extends Service<SignedDocument> {
SignedDocument getForEntity(DataModel entity);
SignedDocumentItem getItem(DataModel entity, long reportId);
void addFromApprove(SignedDocument document);
}
@@ -1,13 +1,25 @@
package info.bukova.isspst.services.signeddocs;
import info.bukova.isspst.Module;
import info.bukova.isspst.ModuleUtils;
import info.bukova.isspst.data.DataModel;
import info.bukova.isspst.data.SignedDocument;
import info.bukova.isspst.data.SignedDocumentItem;
import info.bukova.isspst.services.AbstractOwnedService;
import info.bukova.isspst.services.LazyLoader;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.ServletContext;
public class SignedDocumentServiceImpl extends AbstractOwnedService<SignedDocument> implements SignedDocumentService {
@Autowired
private ServletContext servletContext;
@LazyLoader("grid")
@Transactional
public void lazyLoadItems(SignedDocument signedDocument) {
@@ -19,4 +31,45 @@ public class SignedDocumentServiceImpl extends AbstractOwnedService<SignedDocume
Hibernate.initialize(sd.getItems());
signedDocument.setItems(sd.getItems());
}
@Override
@Transactional
public SignedDocument getForEntity(DataModel entity) {
Module module = ModuleUtils.getModule(entity, servletContext);
if (module == null) {
return null;
}
Query q = dao.getQuery("from SignedDocument where moduleName = :module and recordId = :record");
q.setParameter("module", module.getId());
q.setParameter("record", entity.getId());
return (SignedDocument) q.uniqueResult();
}
@Override
@Transactional
public SignedDocumentItem getItem(DataModel entity, long reportId) {
SignedDocument doc = getForEntity(entity);
if (doc == null) {
return null;
}
for (SignedDocumentItem item : doc.getItems()) {
if (item.getReportId() == reportId) {
return item;
}
}
return null;
}
@Override
public void addFromApprove(SignedDocument document) {
super.add(document);
}
}
@@ -19,9 +19,9 @@ public abstract class AbstractFileStorage<T> implements FileStorage<T> {
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new StorageException("File cannot be found: " + file.getName(), e.getCause());
} catch (IOException e) {
e.printStackTrace();
throw new StorageException("Cannot write file: " + file.getName(), e.getCause());
} finally {
try {
if (os != null) {
@@ -0,0 +1,10 @@
package info.bukova.isspst.storage;
import info.bukova.isspst.data.SignedDocumentItem;
/**
* @author Pepa Rokos
*/
public interface ReportFileStorage extends FileStorage<SignedDocumentItem> {
}
@@ -0,0 +1,75 @@
package info.bukova.isspst.storage;
import info.bukova.isspst.data.SignedDocumentItem;
import java.io.File;
import java.util.UUID;
/**
* @author Pepa Rokos
*/
public class ReportFileStorageImpl extends AbstractFileStorage<SignedDocumentItem> implements ReportFileStorage {
private String rootPath;
public void setRootPath(String rootPath) {
this.rootPath = rootPath;
}
private String getFilePath(String fileName) {
return rootPath + "/" + fileName;
}
@Override
public void saveFile(byte[] data, SignedDocumentItem signedDocumentItem) {
String fileName = signedDocumentItem.getFileName();
if (fileName == null) {
fileName = UUID.randomUUID().toString() + ".pdf";
}
saveFileDataToPath(data, getFilePath(fileName));
signedDocumentItem.setFileName(fileName);
}
@Override
public void saveFile(File file, SignedDocumentItem fileId) {
throw new UnsupportedOperationException();
}
@Override
public void removeFile(SignedDocumentItem signedDocumentItem) {
removeFileByPath(getFilePath(signedDocumentItem.getFileName()));
}
@Override
public void moveFile(String source, String destination) {
throw new UnsupportedOperationException();
}
@Override
public void createDirectory(String dir) {
throw new UnsupportedOperationException();
}
@Override
public byte[] fileData(SignedDocumentItem signedDocumentItem) {
return fileDataFromPath(getFilePath(signedDocumentItem.getFileName()));
}
@Override
public File file(SignedDocumentItem signedDocumentItem) {
return null;
}
@Override
public boolean dirExists(String path) {
return false;
}
@Override
public String serverPath(SignedDocumentItem signedDocumentItem) {
return null;
}
}
@@ -3,20 +3,22 @@ package info.bukova.isspst.ui.signeddocs;
import info.bukova.isspst.data.SignedDocument;
import info.bukova.isspst.data.SignedDocumentItem;
import info.bukova.isspst.filters.SignedDocumentFilter;
import info.bukova.isspst.reporting.ReportDefinition;
import info.bukova.isspst.services.signeddocs.SignedDocumentService;
import info.bukova.isspst.ui.ListViewModel;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Window;
import java.util.ArrayList;
import java.util.List;
public class SignedDocsList extends ListViewModel<SignedDocument>
{
@@ -30,6 +32,9 @@ public class SignedDocsList extends ListViewModel<SignedDocument>
protected List<SignedDocumentItem> signedDocumentItems;
@WireVariable
private ReportDefinition reportDefinition;
@Init(superclass = true)
public void initSignedDocsList() {
service = signedDocumentService;
@@ -70,4 +75,12 @@ public class SignedDocsList extends ListViewModel<SignedDocument>
this.signedDocumentItems = new ArrayList<SignedDocumentItem>();
}
}
@Command
public void onOpen(@BindingParam("item") SignedDocumentItem item) {
reportDefinition.clear();
reportDefinition.setSignedDocItem(item);
Window reportWin = (Window) Executions.createComponents("/app/reporting/report.zul", null, null);
reportWin.doModal();
}
}