Java tutorial
package com.disney.opa.fhb.util; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import javax.validation.ValidationException; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringEscapeUtils; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.glassfish.jersey.media.multipart.FormDataMultiPart; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.disney.opa.bean.UserInfo; import com.disney.opa.fhb.dom.Document; import com.disney.opa.fhb.dom.FacilityContact; import com.disney.opa.fhb.dom.FacilityDocument; import com.disney.opa.fhb.dom.LicenseeFacility; import com.disney.opa.fhb.json.request.SaveFacilityRequest; import com.disney.opa.service.UtilityService; import com.disney.opa.util.AttachmentUtils; import com.disney.opa.util.StringUtils; import com.disney.opa.util.Utils; import com.disney.opa.util.Validator; /** * This class is an utility class for documents related functionality. * */ @Component public class FacilityUtil { @Autowired private UtilityService utilityService; @Autowired private AttachmentUtils attachmentUtils; /** * This method is to get relative file path on server * * @param document * @return relate file path * @throws Exception */ public String getFacilityDocumentFileNameOnServer(Document document) throws Exception { String fileNameOnServer = File.separator + document.getLicenseeId(); if (document.getDocumentType() != FHBConstants.DOC_TYPE_RECALL_PLAN) { fileNameOnServer += File.separator + document.getFacilityId() + File.separator + document.getDocumentType(); } String newFileName = String .valueOf(document.getDocumentId() != null ? document.getDocumentId() : System.nanoTime()); String fileExt = attachmentUtils.getFileExtension(document.getFileName()); if (StringUtils.isNotEmpty(fileExt)) { newFileName += "." + fileExt; } fileNameOnServer += File.separator + newFileName; return fileNameOnServer; } /** * This method is to get facility document full path on server * * @param fileNameOnServer * @return * @throws Exception */ public String getFacilityDocumentServerPath(String fileNameOnServer) throws Exception { String serverPath = utilityService.getOPAPropertyValue("RMS_NAS_PATH"); if (StringUtils.isNotEmpty(serverPath) && serverPath.endsWith("/")) { serverPath = serverPath.substring(0, serverPath.length() - 1); } return (serverPath + fileNameOnServer); } /** * This method is to save the file to disk * * @param document * @throws Exception */ public void saveFacilityDocumentOnServer(Document document) throws Exception { String fileNameOnServer = getFacilityDocumentFileNameOnServer(document); String serverPath = getFacilityDocumentServerPath(fileNameOnServer); FileUtils.writeByteArrayToFile(new File(serverPath), document.getFile()); document.setFileNameOnServer(fileNameOnServer); document.setServerPath(serverPath); } /** * This method is to rename the file with document id * * @param document * @throws Exception */ public void renameFacilityDocumentOnServer(Document document) throws Exception { File originalFile = new File(document.getServerPath()); String fileNameOnServer = getFacilityDocumentFileNameOnServer(document); String serverPath = getFacilityDocumentServerPath(fileNameOnServer); File newFile = new File(serverPath); if (!originalFile.renameTo(newFile)) { throw new Exception("Rename file failed, originalFile: " + originalFile + ", newFile: " + newFile); } document.setFileNameOnServer(fileNameOnServer); document.setServerPath(serverPath); } /** * This method is to get relative file path on server * * @param document * @return relate file path * @throws Exception */ public String getSQDocumentFileNameOnServer(Document document, int productId) throws Exception { String fileNameOnServer = attachmentUtils.getRelativeFilePath(productId); String newFileName = String .valueOf(document.getDocumentId() != null ? document.getDocumentId() : System.nanoTime()); String fileExt = attachmentUtils.getFileExtension(document.getFileName()); if (StringUtils.isNotEmpty(fileExt)) { newFileName += "." + fileExt; } fileNameOnServer += File.separator + newFileName; return fileNameOnServer; } /** * This method is to save the SQ document file to disk * * @param document * @throws Exception */ public void saveSQDocumentOnServer(Document document, int productId) throws Exception { String fileNameOnServer = getSQDocumentFileNameOnServer(document, productId); String serverPath = getSQDocumentServerPath(fileNameOnServer); FileUtils.writeByteArrayToFile(new File(serverPath), document.getFile()); document.setFileNameOnServer(fileNameOnServer); document.setServerPath(serverPath); } /** * This method is to get facility document full path on server * * @param fileNameOnServer * @return * @throws Exception */ public String getSQDocumentServerPath(String fileNameOnServer) throws Exception { String serverPath = attachmentUtils.getRootAttachmentPath(); if (StringUtils.isNotEmpty(serverPath) && serverPath.endsWith("/")) { serverPath = serverPath.substring(0, serverPath.length() - 1); } return (serverPath + fileNameOnServer); } /** * This method is to rename the file with document id * * @param document * @throws Exception */ public void renameSQDocumentOnServer(Document document, int productId) throws Exception { File originalFile = new File(document.getServerPath()); String fileNameOnServer = getSQDocumentFileNameOnServer(document, productId); String serverPath = getSQDocumentServerPath(fileNameOnServer); File newFile = new File(serverPath); if (!originalFile.renameTo(newFile)) { throw new Exception("Rename file failed, originalFile: " + originalFile + ", newFile: " + newFile); } document.setFileNameOnServer(fileNameOnServer); document.setServerPath(serverPath); } /** * This method is to create licensee facility object from save facility request object * * @param saveReFacilityRequest * @return licensee facility object */ public LicenseeFacility getLicenseeFacility(SaveFacilityRequest saveReFacilityRequest) { if (saveReFacilityRequest.getId() == 0 && saveReFacilityRequest.getLicenseeId() == 0) { throw new ValidationException("Licensee id is required to create a new facility."); } if (StringUtils.isEmpty(saveReFacilityRequest.getName())) { throw new ValidationException("Facility name is required."); } if (StringUtils.isEmpty(saveReFacilityRequest.getAddress1())) { throw new ValidationException("Facility address1 is required."); } if (StringUtils.isEmpty(saveReFacilityRequest.getCountryCode())) { throw new ValidationException("Facility country is required."); } // Removing state required check per request JJG 09/15/2015 /*if (StringUtils.isEmpty(saveReFacilityRequest.getState())) { throw new ValidationException("Facility state is required."); }*/ if (StringUtils.isEmpty(saveReFacilityRequest.getCity())) { throw new ValidationException("Facility city is required."); } if (StringUtils.isEmpty(saveReFacilityRequest.getPostalCode())) { throw new ValidationException("Facility postal code is required."); } LicenseeFacility facility = new LicenseeFacility(); facility.setId(saveReFacilityRequest.getId()); facility.setName(saveReFacilityRequest.getName()); facility.setAddress1(saveReFacilityRequest.getAddress1()); facility.setAddress2(saveReFacilityRequest.getAddress2()); facility.setCountryCode(saveReFacilityRequest.getCountryCode()); facility.setState(saveReFacilityRequest.getState()); facility.setCity(saveReFacilityRequest.getCity()); facility.setPostalCode(saveReFacilityRequest.getPostalCode()); facility.setLicenseeId(saveReFacilityRequest.getLicenseeId()); return facility; } /** * This method is to create licensee facility stub object from save facility request object * * @param saveReFacilityRequest * @return licensee facility object */ public LicenseeFacility getLicenseeFacilityStub(SaveFacilityRequest saveReFacilityRequest) { if (saveReFacilityRequest.getId() == 0 && saveReFacilityRequest.getLicenseeId() == 0) { throw new ValidationException("Licensee id is required to create a new facility."); } LicenseeFacility facility = new LicenseeFacility(); facility.setId(saveReFacilityRequest.getId()); facility.setName(saveReFacilityRequest.getName()); facility.setAddress1(saveReFacilityRequest.getAddress1()); facility.setAddress2(saveReFacilityRequest.getAddress2()); facility.setCountryCode(saveReFacilityRequest.getCountryCode()); facility.setState(saveReFacilityRequest.getState()); facility.setCity(saveReFacilityRequest.getCity()); facility.setPostalCode(saveReFacilityRequest.getPostalCode()); facility.setLicenseeId(saveReFacilityRequest.getLicenseeId()); return facility; } /** * This method is to create list of facility contacts from save facility request object * * @param saveReFacilityRequest * @return list of facility contacts */ public List<FacilityContact> getFacilityContacts(SaveFacilityRequest saveReFacilityRequest) { List<FacilityContact> facilityContacts = new ArrayList<FacilityContact>(); if (saveReFacilityRequest.getContacts() != null) { for (SaveFacilityRequest.Contact contact : saveReFacilityRequest.getContacts()) { int id = contact.getId(); int type = contact.getTypeId(); if (StringUtils.isEmpty(contact.getFirstName()) || StringUtils.isEmpty(contact.getLastName())) { throw new ValidationException( "First or last name missing for contact type: " + type + " and Id: " + id); } if ((type == 10 || type == 11) && (StringUtils.isEmpty(contact.getTitle()) || StringUtils.isEmpty(contact.getPhone()) || StringUtils.isEmpty(contact.getEmail()))) { throw new ValidationException( "Required field value missing for contact type: " + type + " and Id: " + id); } if (StringUtils.isNotEmpty(contact.getEmail()) && !Validator.isValidEmail(contact.getEmail())) { throw new ValidationException("Invalid email: " + contact.getEmail()); } if (StringUtils.isNotEmpty(contact.getPhone()) && !Validator.isValidPhoneNumber(contact.getPhone())) { throw new ValidationException("Invalid phone number: " + contact.getPhone()); } FacilityContact facilityContact = new FacilityContact(); facilityContact.setId(contact.getId()); facilityContact.setTitle(contact.getTitle()); facilityContact.setFirstName(contact.getFirstName()); facilityContact.setLastName(contact.getLastName()); facilityContact.setPhone(contact.getPhone()); facilityContact.setEmail(contact.getEmail()); facilityContact.setTypeId(contact.getTypeId()); facilityContacts.add(facilityContact); } } return facilityContacts; } /** * This method is to create list of facility contacts Stub from save facility request object * * @param saveReFacilityRequest * @return list of facility contacts */ public List<FacilityContact> getFacilityContactsStub(SaveFacilityRequest saveReFacilityRequest) { List<FacilityContact> facilityContacts = new ArrayList<FacilityContact>(); if (saveReFacilityRequest.getContacts() != null) { for (SaveFacilityRequest.Contact contact : saveReFacilityRequest.getContacts()) { @SuppressWarnings("unused") int id = contact.getId(); @SuppressWarnings("unused") int type = contact.getTypeId(); FacilityContact facilityContact = new FacilityContact(); facilityContact.setId(contact.getId()); facilityContact.setTitle(contact.getTitle()); facilityContact.setFirstName(contact.getFirstName()); facilityContact.setLastName(contact.getLastName()); facilityContact.setPhone(contact.getPhone()); facilityContact.setEmail(contact.getEmail()); facilityContact.setTypeId(contact.getTypeId()); facilityContacts.add(facilityContact); } } return facilityContacts; } /** * This method is to get uploaded file name * * @param part * @return uploaded file name */ public String getFileName(FormDataBodyPart part) { String fileName = Utils.decodeUTF8(part.getContentDisposition().getFileName()); return StringEscapeUtils.unescapeHtml(fileName); } /** * This method is to get document expiration date from audit date * * @param auditDate * @return expiration date */ public Date getExpirationDate(Date auditDate) { Calendar cal = Calendar.getInstance(); cal.setTime(auditDate); cal.add(Calendar.MONTH, 12); return cal.getTime(); } /** * This method is to parse issue date into audit date * * @param issueDate * @param userInfo * @return audit date * @throws Exception */ public Date getAuditDate(String issueDate, UserInfo userInfo) throws Exception { Locale locale = Locale.US; if (userInfo != null) { locale = userInfo.getLocale(); } return new SimpleDateFormat("MMM. dd, yyyy", locale).parse(issueDate); } /** * This method is used to create file download response * * @param file * @param fileName * @return file download response * @throws Exception */ public Response getDownloadResponse(File file, String fileName) throws Exception { ResponseBuilder response = Response.ok((Object) file); response.header("Content-Disposition", "attachment; filename=\"" + Utils.encodeUTF8(Utils.decodeUTF8(fileName)) + "\"; filename*=utf-8''" + Utils.encodeUTF8(Utils.decodeUTF8(fileName))); response.header("Cache-Control", "no-cache"); response.header("Set-Cookie", "fileDownload=true; Path=/"); return response.build(); } /** * This method is to get initial document * * @param multiPart * @return initial document object */ public Document getDocument(FormDataMultiPart multiPart) { Document document = new Document(); FormDataBodyPart part = multiPart.getField("uploadedFile"); document.setFile(part.getEntityAs(byte[].class)); String fileName = getFileName(part); document.setFileName(fileName); document.setDocumentName(fileName); document.setAuditDate(new Date()); document.setExpirationDate(getExpirationDate(document.getAuditDate())); return document; } public Document getSQDocumentForDownload(FacilityDocument facdoc) throws Exception { Document document = new Document(); document.setFileName(facdoc.getFileName()); document.setServerPath(getSQDocumentServerPath(facdoc.getFilePath())); return document; } public SimpleDateFormat getUserDateFormat(UserInfo userInfo) throws Exception { Locale locale = Locale.US; if (userInfo != null) { locale = userInfo.getLocale(); } return new SimpleDateFormat("MMM. dd, yyyy", locale); } }