Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package bean; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import com.liferay.counter.service.CounterLocalServiceUtil; import com.liferay.faces.portal.context.LiferayFacesContext; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.json.JSONFactoryUtil; import com.liferay.portal.kernel.json.JSONObject; import com.liferay.portal.kernel.util.HtmlUtil; import com.liferay.portal.model.User; import com.liferay.portal.model.UserNotificationEvent; import com.liferay.portal.service.OrganizationLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.service.UserNotificationEventLocalServiceUtil; import com.liferay.portal.service.persistence.UserNotificationEventUtil; import java.io.FileOutputStream; import java.io.IOException; import java.io.Serializable; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.UUID; import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.SessionScoped; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import model.*; import service.*; /** * * @author mis05 */ @ManagedBean @SessionScoped public class ReportingBean implements Serializable { @PersistenceUnit EntityManagerFactory entityManagerFactory; @EJB private CoopActivityFacadeREST coopActivityFacadeREST; private CoopActivity activity = new CoopActivity(); private CoopActivity selectedActivity = new CoopActivity(); private List<CoopActivity> activityList; @EJB private CoopActivityTypeFacadeREST coopActivityTypeFacadeREST; private CoopActivityType activityType = new CoopActivityType(); private CoopActivityType selectedActivityType = new CoopActivityType(); private List<CoopActivityType> activityTypeList; @EJB private CoopInfoFacadeREST coopInfoFacadeREST; private CoopInfo info = new CoopInfo(); private List<CoopInfo> infoList; @EJB private CoopMemberFacadeREST coopMemberFacadeREST; private CoopMember member = new CoopMember(); @EJB private CoopMemberUserFacadeREST coopMemberUserFacadeREST; private CoopMemberUser memberUser = new CoopMemberUser(); @EJB private CoopOrgUnitFacadeREST coopOrgUnitFacadeREST; private CoopOrgUnit orgUnit = new CoopOrgUnit(); @EJB private CoopRepActFacadeREST coopRepActFacadeREST; private CoopRepAct repAct = new CoopRepAct(); private CoopRepAct selectedRepAct = new CoopRepAct(); private List<CoopRepAct> repActList; @EJB private CoopReportFacadeREST coopReportFacadeREST; private CoopReport report = new CoopReport(); private CoopReport selectedReport = new CoopReport(); private List<CoopReport> reportList; @EJB private CoopReportCircFacadeREST coopReportCircFacadeREST; private CoopReportCirc reportCirc = new CoopReportCirc(); private CoopReportCirc selectedReportCirc = new CoopReportCirc(); @EJB private CoopReportTypeFacadeREST coopReportTypeFacadeREST; private CoopReportType reportType = new CoopReportType(); private CoopReportType selectedReportType = new CoopReportType(); private List<CoopReportType> reportTypeList; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); CustomViewHandler view = new CustomViewHandler(); DateStringParse dateStringParse = new DateStringParse(); Calendar c = Calendar.getInstance(); private UAgentInfo agent; private Integer reportTypeValue = 0, reportValue = 0, activityTypeValue = 0, activityValue = 0; private String ou, memNo; private Boolean mode = false; long a, b, currentUserId; long[] currentUserOrg; LiferayFacesContext liferayFacesContext; User user, currentUser; List<User> all, notify; Date now; public static final String FILE = "Report" + "-Date" + ".pdf"; public static final String RESULT = "../../../../../" + FILE; @ManagedProperty(value = "#{currentDate}") private Date reportDateEncoded; public Date getReportDateEncoded() { return reportDateEncoded; } public void setReportDateEncoded(Date reportDateEncoded) { this.reportDateEncoded = reportDateEncoded; } /** * Creates a new instance of ReportingBean */ public ReportingBean() { } public void init() throws PortalException, SystemException { report.setReportDateEncoded(reportDateEncoded); infoList = coopInfoFacadeREST.findAll(); liferayFacesContext = LiferayFacesContext.getInstance(); currentUser = liferayFacesContext.getUser(); currentUserOrg = currentUser.getOrganizationIds(); now = new Date(); currentUserId = liferayFacesContext.getUserId(); reportTypeList = entityManagerFactory.createEntityManager().createQuery( "SELECT r FROM CoopReportType r WHERE r.reportTypeCode <> 11 AND r.reportTypeCode <> 3 ORDER BY r.reportType") .getResultList(); activityTypeList = coopActivityTypeFacadeREST.findAll(); /*for (int counter = 0; counter <= currentUserOrg.length - 1; counter++) { try { memNo = (String) entityManagerFactory.createEntityManager().createQuery("SELECT c.memNo FROM CoopMemberUser c WHERE c.userId = " + currentUserId).getResultList().get(0); member.setMemNo(memNo); //choose ou for user with multiple ou ou = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getName(); orgUnit.setOuCode(ou); a = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[counter]).getOrganizationId(); b = OrganizationLocalServiceUtil.getParentOrganizations(a).get(0).getOrganizationId(); notify = UserLocalServiceUtil.getOrganizationUsers(b); } catch (Exception e) { } }*/ try { memNo = (String) entityManagerFactory.createEntityManager() .createQuery("SELECT c.memNo FROM CoopMemberUser c WHERE c.userId = " + currentUserId) .getResultList().get(0); member.setMemNo(memNo); ou = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[0]).getName(); orgUnit.setOuCode(ou); a = OrganizationLocalServiceUtil.getOrganization(currentUserOrg[0]).getOrganizationId(); b = OrganizationLocalServiceUtil.getParentOrganizations(a).get(0).getOrganizationId(); notify = UserLocalServiceUtil.getOrganizationUsers(b); } catch (Exception e) { } ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); String userAgent = externalContext.getRequestHeaderMap().get("User-Agent"); String accept = externalContext.getRequestHeaderMap().get("Accept"); setAgent(new UAgentInfo(userAgent, accept)); /* 1. standard/specific && structured/unstructured data && functions/template: -activity report ---education ---event celebration ---meeting ---political action ---project implementation ---propaganda ---recruitment ---sdcc election ---social investigation ---training -report ---assessment ---financial report ---plans and programs ---social investigation ---study ---summing-up ---transcription 2. per ou viewing report permission 3. migrate to alfresco */ } public void beanclear() { FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("reportingBean", null); } /* GETTER AND SETTER */ public CoopActivityFacadeREST getCoopActivityFacadeREST() { return coopActivityFacadeREST; } public void setCoopActivityFacadeREST(CoopActivityFacadeREST coopActivityFacadeREST) { this.coopActivityFacadeREST = coopActivityFacadeREST; } public CoopActivity getActivity() { return activity; } public void setActivity(CoopActivity activity) { this.activity = activity; } public CoopActivity getSelectedActivity() { return selectedActivity; } public void setSelectedActivity(CoopActivity selectedActivity) { this.selectedActivity = selectedActivity; } public CoopActivityTypeFacadeREST getCoopActivityTypeFacadeREST() { return coopActivityTypeFacadeREST; } public void setCoopActivityTypeFacadeREST(CoopActivityTypeFacadeREST coopActivityTypeFacadeREST) { this.coopActivityTypeFacadeREST = coopActivityTypeFacadeREST; } public CoopActivityType getActivityType() { return activityType; } public void setActivityType(CoopActivityType activityType) { this.activityType = activityType; } public CoopActivityType getSelectedActivityType() { return selectedActivityType; } public void setSelectedActivityType(CoopActivityType selectedActivityType) { this.selectedActivityType = selectedActivityType; } public CoopInfoFacadeREST getCoopInfoFacadeREST() { return coopInfoFacadeREST; } public void setCoopInfoFacadeREST(CoopInfoFacadeREST coopInfoFacadeREST) { this.coopInfoFacadeREST = coopInfoFacadeREST; } public CoopInfo getInfo() { return info; } public void setInfo(CoopInfo info) { this.info = info; } public List<CoopInfo> getInfoList() { return infoList; } public void setInfoList(List<CoopInfo> infoList) { this.infoList = infoList; } public CoopMemberFacadeREST getCoopMemberFacadeREST() { return coopMemberFacadeREST; } public void setCoopMemberFacadeREST(CoopMemberFacadeREST coopMemberFacadeREST) { this.coopMemberFacadeREST = coopMemberFacadeREST; } public CoopMember getMember() { return member; } public void setMember(CoopMember member) { this.member = member; } public CoopMemberUserFacadeREST getCoopMemberUserFacadeREST() { return coopMemberUserFacadeREST; } public void setCoopMemberUserFacadeREST(CoopMemberUserFacadeREST coopMemberUserFacadeREST) { this.coopMemberUserFacadeREST = coopMemberUserFacadeREST; } public CoopMemberUser getMemberUser() { return memberUser; } public void setMemberUser(CoopMemberUser memberUser) { this.memberUser = memberUser; } public CoopOrgUnitFacadeREST getCoopOrgUnitFacadeREST() { return coopOrgUnitFacadeREST; } public void setCoopOrgUnitFacadeREST(CoopOrgUnitFacadeREST coopOrgUnitFacadeREST) { this.coopOrgUnitFacadeREST = coopOrgUnitFacadeREST; } public CoopOrgUnit getOrgUnit() { return orgUnit; } public void setOrgUnit(CoopOrgUnit orgUnit) { this.orgUnit = orgUnit; } public CoopRepActFacadeREST getCoopRepActFacadeREST() { return coopRepActFacadeREST; } public void setCoopRepActFacadeREST(CoopRepActFacadeREST coopRepActFacadeREST) { this.coopRepActFacadeREST = coopRepActFacadeREST; } public CoopRepAct getRepAct() { return repAct; } public void setRepAct(CoopRepAct repAct) { this.repAct = repAct; } public CoopRepAct getSelectedRepAct() { return selectedRepAct; } public void setSelectedRepAct(CoopRepAct selectedRepAct) { this.selectedRepAct = selectedRepAct; } public CoopReportFacadeREST getCoopReportFacadeREST() { return coopReportFacadeREST; } public void setCoopReportFacadeREST(CoopReportFacadeREST coopReportFacadeREST) { this.coopReportFacadeREST = coopReportFacadeREST; } public CoopReport getReport() { return report; } public void setReport(CoopReport report) { this.report = report; } public CoopReport getSelectedReport() { return selectedReport; } public void setSelectedReport(CoopReport selectedReport) { this.selectedReport = selectedReport; } public List<CoopReport> getReportList() { return reportList; } public void setReportList(List<CoopReport> reportList) { this.reportList = reportList; } public CoopReportCircFacadeREST getCoopReportCircFacadeREST() { return coopReportCircFacadeREST; } public void setCoopReportCircFacadeREST(CoopReportCircFacadeREST coopReportCircFacadeREST) { this.coopReportCircFacadeREST = coopReportCircFacadeREST; } public CoopReportCirc getReportCirc() { return reportCirc; } public void setReportCirc(CoopReportCirc reportCirc) { this.reportCirc = reportCirc; } public CoopReportCirc getSelectedReportCirc() { return selectedReportCirc; } public void setSelectedReportCirc(CoopReportCirc selectedReportCirc) { this.selectedReportCirc = selectedReportCirc; } public CoopReportTypeFacadeREST getCoopReportTypeFacadeREST() { return coopReportTypeFacadeREST; } public void setCoopReportTypeFacadeREST(CoopReportTypeFacadeREST coopReportTypeFacadeREST) { this.coopReportTypeFacadeREST = coopReportTypeFacadeREST; } public CoopReportType getReportType() { return reportType; } public void setReportType(CoopReportType reportType) { this.reportType = reportType; } public CoopReportType getSelectedReportType() { return selectedReportType; } public void setSelectedReportType(CoopReportType selectedReportType) { this.selectedReportType = selectedReportType; } public List<CoopReportType> getReportTypeList() { return reportTypeList; } public void setReportTypeList(List<CoopReportType> reportTypeList) { this.reportTypeList = reportTypeList; } public UAgentInfo getAgent() { return agent; } public void setAgent(UAgentInfo agent) { this.agent = agent; } public Integer getReportTypeValue() { return reportTypeValue; } public void setReportTypeValue(Integer reportTypeValue) { this.reportTypeValue = reportTypeValue; } public Integer getReportValue() { return reportValue; } public void setReportValue(Integer reportValue) { this.reportValue = reportValue; } public String getOu() { return ou; } public void setOu(String ou) { this.ou = ou; } public String getMemNo() { return memNo; } public void setMemNo(String memNo) { this.memNo = memNo; } public Boolean getMode() { return mode; } public void setMode(Boolean mode) { this.mode = mode; } public List<CoopActivity> getActivityList() { return activityList; } public void setActivityList(List<CoopActivity> activityList) { this.activityList = activityList; } public List<CoopActivityType> getActivityTypeList() { return activityTypeList; } public void setActivityTypeList(List<CoopActivityType> activityTypeList) { this.activityTypeList = activityTypeList; } public Integer getActivityTypeValue() { return activityTypeValue; } public void setActivityTypeValue(Integer activityTypeValue) { this.activityTypeValue = activityTypeValue; } public Integer getActivityValue() { return activityValue; } public void setActivityValue(Integer activityValue) { this.activityValue = activityValue; } public List<CoopRepAct> getRepActList() { return repActList; } public void setRepActList(List<CoopRepAct> repActList) { this.repActList = repActList; } /* METHODS */ public static String formatDate(Date currentDate) { if (currentDate == null) { currentDate = new Date(); } DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy"); return dateFormat.format(currentDate); } public void reportTypeCh() { reportList = entityManagerFactory.createEntityManager().createQuery( "SELECT r FROM CoopReport r JOIN r.reportTypeCode s WHERE s.reportTypeCode <> 1 AND s.reportTypeCode = : " + reportTypeValue) .getResultList(); } public void activityTypeChView() { //repActList = entityManagerFactory.createEntityManager().createQuery("SELECT r FROM CoopRepAct r JOIN r.actNum s JOIN r.reportNum t WHERE t.reportTypeCode = 1 AND s.actTypeCode = : " + activityTypeValue).getResultList(); //repActList = entityManagerFactory.createEntityManager().createQuery("SELECT ra FROM CoopRepAct WHERE actNum NOT NULL").getResultList(); //reportList = entityManagerFactory.createEntityManager().createQuery("SELECT r FROM CoopReport r WHERE r.reportTypeCode = 1 AND (SELECT s FROM CoopActivity s WHERE s.actTypeCode = : " + activityTypeValue + ")").getResultList(); //reportList = entityManagerFactory.createEntityManager().createQuery("SELECT r FROM CoopReport r WHERE (SELECT a FROM CoopActivity a WHERE a.actTypeCode = 3)").getResultList(); //reportList = entityManagerFactory.createEntityManager().createQuery("SELECT r FROM CoopReport r WHERE (SELECT a FROM CoopActivity a WHERE a.actTypeCode = : " + activityTypeValue + ") AND (SELECT ra FROM CoopRepAct ra JOIN ra.actNum raa WHERE raa.actNum = '')").getResultList(); //reportList = coopReportFacadeREST.findAll(); reportList = entityManagerFactory.createEntityManager() .createQuery("SELECT r FROM CoopReport a JOIN CoopRepAct b ON a.reportNum = b.reportNum") .getResultList(); ///////////////////////////////////////////sql OK//////////////////////////////////////////////////// //SELECT r,s FROM coop_report r,coop_activity s WHERE r.report_type_code = 1 AND s.act_type_code = 3 //SELECT * FROM coop_report r,coop_activity s WHERE r.report_type_code = 1 AND s.act_type_code = 3 ///////////////////////////////////////////////////////////////////////////////////////////////////// /* 1. view select report type [reporttypevalue=1]:select normal report select report date [query=1] 2. view select report type [reporttypevalue=1]:select activity report select activity type [activitytypevalue=2] select repact date [query=1 query=2] */ } public void activityTypeChCreate() { activityList = entityManagerFactory.createEntityManager().createQuery( "SELECT a FROM CoopActivity a JOIN a.actTypeCode b WHERE b.actTypeCode = : " + activityTypeValue) .getResultList(); } public String saveReport() throws SystemException, DocumentException, IOException { saveToReport(); if (!(activityTypeValue == 0)) { saveToRepAct(); } saveToReportCirc(); passValue(); sendMessage(); beanclear(); return "viewReport.xhtml?faces-redirect=true"; } public void saveToReport() { report.setReportNum(report.getReportNum()); report.setReportDtl(report.getReportDtl()); report.setReportDateFrom(report.getReportDateFrom()); report.setReportDateTo(report.getReportDateTo()); report.setReportDateEncoded(reportDateEncoded); report.setMemNo(member); reportType.setReportTypeCode(reportTypeValue); report.setReportTypeCode(reportType); report.setOuCode(orgUnit); coopReportFacadeREST.create(report); } public void saveToRepAct() { repAct.setReportNum(report); repAct.setActNum(selectedActivity); coopRepActFacadeREST.create(repAct); } public void saveToReportCirc() { reportCirc.setReportNum(report); coopReportCircFacadeREST.create(reportCirc); } public void sendMessage() throws SystemException { for (User users : notify) { JSONObject payloadJSON = JSONFactoryUtil.createJSONObject(); payloadJSON.put("userId", currentUser.getUserId()); payloadJSON.put("fullName", currentUser.getFullName()); UserNotificationEvent userNotificationEvent = UserNotificationEventUtil .create(CounterLocalServiceUtil.increment()); userNotificationEvent.setUuid(UUID.randomUUID().toString()); userNotificationEvent.setCompanyId(users.getCompanyId()); userNotificationEvent.setUserId(users.getUserId()); userNotificationEvent.setType(CustomNotificationHandler.PORTLET_ID); userNotificationEvent.setTimestamp(now.getTime()); userNotificationEvent.setDeliverBy(users.getUserId()); userNotificationEvent.setDelivered(false); userNotificationEvent.setPayload(payloadJSON.toString()); userNotificationEvent.setArchived(false); UserNotificationEventLocalServiceUtil.addUserNotificationEvent(userNotificationEvent); } } public void loadDataView() { if (!(activityTypeValue == 0)) { selectedReport = new CoopReport(); selectedRepAct = new CoopRepAct(); selectedReportCirc = new CoopReportCirc(); selectedActivity = new CoopActivity(); reportLoad(); repActLoad(); reportCircLoad(); activityLoad(); } else { selectedReport = new CoopReport(); selectedReportCirc = new CoopReportCirc(); reportLoad(); reportCircLoad(); } } public void loadDataCreate() { selectedActivity = new CoopActivity(); activityLoad(); } public void activityLoad() { CoopActivity a = (CoopActivity) entityManagerFactory.createEntityManager() .createQuery("SELECT a FROM CoopActivity a WHERE a.actNum = : " + activityValue).getResultList() .get(0); selectedActivity = a; } public void reportLoad() { CoopReport r = (CoopReport) entityManagerFactory.createEntityManager() .createQuery("SELECT r FROM CoopReport r WHERE r.reportNum = : " + reportValue).getResultList() .get(0); selectedReport = r; } public void repActLoad() { CoopRepAct r = (CoopRepAct) entityManagerFactory.createEntityManager() .createQuery("SELECT r FROM CoopRepAct r JOIN r.reportNum s WHERE s.reportNum = : " + reportValue) .getResultList().get(0); selectedRepAct = r; } public void reportCircLoad() { CoopReportCirc r = (CoopReportCirc) entityManagerFactory.createEntityManager() .createQuery( "SELECT r FROM CoopReportCirc r JOIN r.reportNum s WHERE s.reportNum = : " + reportValue) .getResultList().get(0); selectedReportCirc = r; } public String navigateView() { beanclear(); return "viewReport.xhtml?faces-redirect=true"; } public String navigateCreate() { beanclear(); return "createReport.xhtml?faces-redirect=true"; } public String navigatePrint() { return "printReport.xhtml?faces-redirect=true"; } public void passValue() throws DocumentException, IOException { createPdf(RESULT); new ReportingBean().createPdf(RESULT); } public void createPdf(String filename) throws DocumentException, IOException { //String escaped = HtmlUtil.escape(editorVal); //String escapedCss = HtmlUtil.escapeCSS(editorVal); //StringReader stringReader = new StringReader(editorVal); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(filename)); //PdfWriter.getInstance(document, new FileOutputStream(RESULT)); //PdfWriter.getInstance(document, new FileOutputStream("JSF-PDF.pdf")); document.open(); document.add(new Paragraph("insert letter head")); document.add(new Paragraph("insert title")); document.add(new Paragraph("date:")); document.add(new Paragraph("date encoded:")); document.add(new Paragraph("date from" + "-" + "date to")); document.add(new Paragraph("time from" + "-" + "time to")); //document.add(new Paragraph("escaped" + "\n" + editorVal)); //document.add(new Paragraph("escaped()" + "\n" + escaped)); //document.add(new Paragraph("escapedCss()" + "\n" + escapedCss)); document.close(); } }