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 com.olp.jpa.domain.docu.ut.service; import com.olp.jpa.common.PageRequestBean; import com.olp.jpa.common.PageResponseBean; import com.olp.jpa.common.SearchCriteriaBean; import com.olp.jpa.common.SortCriteriaBean; import com.olp.service.ServiceVersion; import com.olp.jpa.domain.docu.ut.repo.EmployeeService; import com.olp.jpa.domain.docu.ut.model.EmpBeanPub; import com.olp.jpa.domain.docu.ut.model.EmployeeBean; import com.olp.jpa.util.JpaUtil; import com.olp.service.FaultUtil; import com.olp.service.ServiceException; import javax.jws.WebService; import javax.ws.rs.Path; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.Future; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Resource; import javax.jws.WebService; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.xml.ws.AsyncHandler; import javax.xml.ws.Response; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; import org.apache.cxf.annotations.UseAsyncMethod; import org.apache.cxf.jaxws.ServerAsyncResponse; import org.apache.cxf.jaxws.context.WrappedMessageContext; import org.apache.cxf.message.Message; import org.apache.cxf.ws.addressing.AddressingProperties; import org.apache.cxf.ws.addressing.EndpointReferenceType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; /** * * @author raghosh */ @Component("empWebService") @WebService(serviceName = "EmpService", endpointInterface = "com.olp.jpa.domain.docu.ut.service.EmpWebService", targetNamespace = "http://trilia-cloud.com/webservices/entity/ut/ut-emp-service", portName = "EmpService_Port") @Path("/employees") public class EmpWebServiceImpl implements EmpWebService { @Resource private WebServiceContext context; @Autowired @Qualifier("empService") private EmployeeService service; @Autowired private ServiceVersion svcVersion; @Override @Path("/") @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public ServiceVersion getVersion() { return (svcVersion); } @Override @Transactional(readOnly = true) @Path("/employee/{empId: \\d+}") @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public EmpBeanPub findOne(@PathParam("empId") Long id) throws ServiceException { EmpBeanPub bean2 = null; try { EmployeeBean bean1 = service.find(id); bean2 = bean1.convertTo(); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException("Exception occured in listAllEmps service", t); throw se; } return (bean2); } @Override @Transactional(readOnly = true) @Path("/employee/{empCode: ^[a-zA-Z]+(\\d+)*}") @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public EmpBeanPub findByAltKey(@PathParam("empCode") String empCode) throws ServiceException { EmpBeanPub bean2 = null; try { EmployeeBean bean1 = service.find(empCode); bean2 = bean1.convertTo(); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException("Exception occured in findEmpByCode service", t); throw se; } return (bean2); } @Override @Transactional @Path("/employee") @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public EmpBeanPub add(EmpBeanPub emp) throws ServiceException { if (emp == null) { ServiceException se = FaultUtil.makeServiceException("Null employee parameter received", new IllegalArgumentException("Null employee parameter received !")); throw se; } if (emp.getEmployeeNumber() == null) { ServiceException se = FaultUtil.makeServiceException("Null employee parameter received", new IllegalArgumentException("Null employee code attribute received !")); throw se; } EmpBeanPub bean2 = null; try { EmployeeBean bean1 = emp.convertTo(); service.add(bean1); emp.setId(bean1.getId()); emp.setRevisionControl(bean1.getRevisionControl()); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException("Exception occurred in addEmp service", t); throw se; } return (bean2); } @Override @Transactional @Path("/employee") @PUT @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public EmpBeanPub update(EmpBeanPub emp) throws ServiceException { if (emp == null) { ServiceException se = FaultUtil.makeServiceException("Exception occured in update service", new IllegalArgumentException("Null employee parameter received !")); throw se; } if (emp.getId() == null || emp.getId() <= 0) { ServiceException se = FaultUtil.makeServiceException("Exception occured in update service", new IllegalArgumentException("Null or invalid employee id")); throw se; } EmployeeBean bean = null; try { bean = service.find(emp.getId()); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in update service while fetching employee object with id " + emp.getId(), t); throw se; } if (bean == null) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in update service - null employee object", new IllegalArgumentException("No employee with id " + emp.getId() + " exists !")); throw se; } if (!bean.getEmployeeNumber().equals(emp.getEmployeeNumber())) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in update service - id & code mismatch", new IllegalArgumentException( "No employee with code " + emp.getEmployeeNumber() + " exists for id " + emp.getId())); throw se; } bean.setFirstName(emp.getFirstName()); bean.setLastName(emp.getLastName()); bean.setNickName(emp.getNickName()); bean.setEmail(emp.getEmail()); try { service.update(bean); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in update service while updating employee object with id " + emp.getId(), t); throw se; } EmpBeanPub bean2 = bean.convertTo(); return (bean2); } @Override @Transactional @Path("/employee/{empId: \\d+}") @DELETE public void delete(@PathParam("empId") Long id) throws ServiceException { if (id == null || id <= 0) { ServiceException se = FaultUtil.makeServiceException("Exception occured in delete service", new IllegalArgumentException("Null employee id parameter received !")); throw se; } try { service.delete(id); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in delete service while deleting employee with id " + id, t); throw se; } } @Override @Transactional @Path("/employee/{empCode: ^[a-zA-Z]+(\\d+)*}") @DELETE public void delete(@PathParam("empCode") String empCode) throws ServiceException { if (empCode == null || "".equals(empCode)) { ServiceException se = FaultUtil.makeServiceException("Exception occured in delete service", new IllegalArgumentException("Null employee code parameter received !")); throw se; } EmployeeBean bean = null; try { bean = service.find(empCode); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in delete service while deleting employee with code " + empCode, t); throw se; } if (bean == null) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in delete service - null employee object", new IllegalArgumentException("No employee with code " + empCode + " exists !")); throw se; } try { service.delete(bean.getId()); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in delete service while deleting employee with code " + empCode, t); throw se; } } @Override @Transactional(readOnly = true) public List<EmpBeanPub> doSimpleSearch(String keywords, Boolean fuzzy, SortCriteriaBean sort, Integer outputMode) throws ServiceException { List<EmpBeanPub> list2 = null; try { List<EmployeeBean> list1 = service.findText(keywords, fuzzy, sort); list2 = convert(list1, outputMode); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException("Exception occured in doSimpleSearch service", t); throw se; } return (list2); } //@Override @Transactional(readOnly = true) @Path("/search") @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public List<EmpBeanPub> doSimpleSearch(@QueryParam("q") String keywords, @QueryParam("fuzzy") Boolean fuzzy, @QueryParam("asc") List<String> ascFields, @QueryParam("desc") List<String> descFields, @QueryParam("outputMode") Integer outputMode) throws ServiceException { Integer mode = (outputMode == null || outputMode < 0) ? 0 : outputMode; SortCriteriaBean sort = new SortCriteriaBean(); if (ascFields != null) sort.setAscOrderFields(ascFields); if (descFields != null) sort.setDescOrderFields(descFields); List<EmployeeBean> list1 = service.findText(keywords, fuzzy, sort); List<EmpBeanPub> list2 = convert(list1, mode); return (list2); } @Override @Transactional(readOnly = true) public PageResponseBean<EmpBeanPub> doSimpleSearch(String keywords, Boolean fuzzy, Integer outputMode, PageRequestBean request) throws ServiceException { if (keywords == null || "".equals(keywords)) { ServiceException se = FaultUtil.makeServiceException( "Exception occured in simple search paged service ", new IllegalArgumentException("No search keyword found !")); throw se; } PageRequest req = JpaUtil.convert(request); Page<EmployeeBean> resp = null; try { resp = service.findText(keywords, fuzzy, req); } catch (Throwable t) { ServiceException se = FaultUtil .makeServiceException("Exception occurred in simple search paged service", t); throw se; } List<EmployeeBean> list1 = resp.getContent(); List<EmpBeanPub> list2 = convert(list1, outputMode); PageResponseBean<EmpBeanPub> response = JpaUtil.tranform(resp, list2); return (response); } //@Override @Transactional(readOnly = true) @Path("/pagedSearch") @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public PageResponseBean<EmpBeanPub> doSimpleSearch(@QueryParam("q") String keywords, @QueryParam("fuzzy") Boolean fuzzy, @QueryParam("outputMode") Integer outputMode, @QueryParam("page") Integer pageNum, @QueryParam("pageSize") Integer pageSize, @QueryParam("asc") List<String> ascFields, @QueryParam("desc") List<String> descFields) throws ServiceException { Integer mode = (outputMode == null || outputMode < 0) ? 0 : outputMode; Integer pageNum2 = (pageNum == null || pageNum <= 0) ? 1 : pageNum; Integer pageSize2 = (pageSize == null || pageSize <= 0) ? DEFAULT_PAGE_SIZE : pageSize; PageRequestBean request = new PageRequestBean(); request.setPageNum(pageNum2); request.setPageSize(0); request.setOffset(pageNum2 * pageSize2); SortCriteriaBean sort = new SortCriteriaBean(); if (ascFields != null) sort.setAscOrderFields(ascFields); if (descFields != null) sort.setDescOrderFields(descFields); request.setSort(sort); PageResponseBean<EmpBeanPub> response = doSimpleSearch(keywords, fuzzy, mode, request); return (response); } @Override @Transactional(readOnly = true) public List<EmpBeanPub> doAdvancedSearch(SearchCriteriaBean search, SortCriteriaBean sort, Integer outputMode) throws ServiceException { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override @Transactional(readOnly = true) public PageResponseBean<EmpBeanPub> doAdvancedSearch(SearchCriteriaBean search, Integer outputMode, PageRequestBean request) throws ServiceException { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override @Transactional(readOnly = true) public PageResponseBean<EmpBeanPub> findAll(Integer outputMode, PageRequestBean request) throws ServiceException { PageRequest req = JpaUtil.convert(request); Page<EmployeeBean> resp = null; try { resp = service.findAll(req); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException("Exception occurred in findAll paged service", t); throw se; } List<EmployeeBean> list1 = resp.getContent(); List<EmpBeanPub> list2 = convert(list1, outputMode); PageResponseBean<EmpBeanPub> response = JpaUtil.tranform(resp, list2); return (response); } @Transactional(readOnly = true) @Path("/pagedList") @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public PageResponseBean<EmpBeanPub> findAll(@QueryParam("outputMode") Integer outputMode, @QueryParam("page") Integer pageNum, @QueryParam("pageSize") Integer pageSize, @QueryParam("asc") List<String> ascFields, @QueryParam("desc") List<String> descFields) throws ServiceException { Integer mode = (outputMode == null || outputMode < 0) ? 0 : outputMode; Integer pageNum2 = (pageNum == null || pageNum <= 0) ? 1 : pageNum; Integer pageSize2 = (pageSize == null || pageSize <= 0) ? DEFAULT_PAGE_SIZE : pageSize; PageRequestBean request = new PageRequestBean(); request.setPageNum(pageNum2); request.setPageSize(0); request.setOffset(pageNum2 * pageSize2); SortCriteriaBean sort = new SortCriteriaBean(); if (ascFields != null) sort.setAscOrderFields(ascFields); if (descFields != null) sort.setDescOrderFields(descFields); request.setSort(sort); PageResponseBean<EmpBeanPub> response = findAll(mode, request); return (response); } @Override @Transactional(readOnly = true) @Path("/list") @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public List<EmpBeanPub> findAll(@QueryParam("outputMode") Integer outputMode) throws ServiceException { ArrayList<EmpBeanPub> list2 = null; try { List<EmployeeBean> list1 = service.findAll(); list2 = list1 == null ? null : new ArrayList<EmpBeanPub>(); for (int i = 0; list1 != null && i < list1.size(); i++) { EmployeeBean bean1 = list1.get(i); EmpBeanPub bean2 = bean1.convertTo(); list2.add(bean2); } } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException("Exception occured in listAllEmps service", t); throw se; } return (list2); } @Override @UseAsyncMethod @Transactional @Path("/bulk") @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public List<EmpBeanPub> addAll(List<EmpBeanPub> list, boolean ignoreError, Integer outputMode) throws ServiceException { Logger logger = Logger.getLogger(getClass().getName()); logger.log(Level.INFO, "******** addAll Sync method called *********"); if (list == null || list.size() < 1) { ServiceException se = FaultUtil.makeServiceException("Null employee parameter received", new IllegalArgumentException("Null employee parameter received")); throw se; } //if (ignoreError) { for (int i = 0; i < list.size(); i++) { EmpBeanPub bean = list.get(i); try { EmployeeBean bean2 = bean.convertTo(); service.add(bean2); bean.setId(bean2.getId()); bean.setRevisionControl(bean2.getRevisionControl()); } catch (Throwable t) { if (ignoreError) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Exception encountered while adding employee with code " + bean.getEmployeeNumber(), t); } else { ServiceException se = FaultUtil.makeServiceException( "Exception occurred in addAllEmp service for employee code " + bean.getEmployeeNumber(), t); throw se; } } } //} return (list); } @Override @UseAsyncMethod @Transactional @Path("/bulk") @DELETE public void deleteAll(boolean ignoreError) throws ServiceException { try { service.deleteAll(ignoreError); } catch (Throwable t) { ServiceException se = FaultUtil.makeServiceException("Exception occurred in deleteAllEmp service", t); throw se; } } @Override @UseAsyncMethod @Transactional @Path("/bulk") @PUT @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public List<EmpBeanPub> updateAll(List<EmpBeanPub> list, boolean ignoreError, Integer outputMode) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override @Transactional public Future<?> addAllAsync(List<EmpBeanPub> list, boolean ignoreError, Integer outputMode, final AsyncHandler<List<EmpBeanPub>> asyncHandler) throws ServiceException { Logger logger = Logger.getLogger(getClass().getName()); logger.log(Level.INFO, "***** Within Async addAll method *****"); final ServerAsyncResponse<List<EmpBeanPub>> r = new ServerAsyncResponse<>(); MessageContext mContext = context.getMessageContext(); Set<String> s = mContext.keySet(); Iterator<String> iter = s == null ? null : s.iterator(); while (iter != null && iter.hasNext()) { logger.log(Level.INFO, "MessageContext property - {0}", iter.next()); } WrappedMessageContext wmc = (WrappedMessageContext) mContext; Message m = wmc.getWrappedMessage(); Set<String> s2 = m.keySet(); Iterator<String> iter2 = s2 == null ? null : s2.iterator(); while (iter2 != null && iter2.hasNext()) { logger.log(Level.INFO, "Message property - {0}", iter2.next()); } AddressingProperties addressProp = (AddressingProperties) mContext .get(org.apache.cxf.ws.addressing.JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_INBOUND); EndpointReferenceType eprType = addressProp.getReplyTo(); return (r); } @Override public Response addAllAsync(List<EmpBeanPub> list, boolean ignoreError, Integer outputMode) throws ServiceException { return (null); } @Override public Future<?> updateAllAsync(List<EmpBeanPub> list, boolean ignoreError, Integer outputMode, final AsyncHandler<List<EmpBeanPub>> asyncHandler) throws ServiceException { return (null); } @Override public Response updateAllAsync(List<EmpBeanPub> list, boolean ignoreError, Integer outputMode) throws ServiceException { return (null); } @Override public Future<?> deleteAllAsync(boolean ignoreError, final AsyncHandler asyncHandler) throws ServiceException { return (null); } @Override public Response deleteAllAsync(boolean ignoreError) throws ServiceException { return (null); } private List<EmpBeanPub> convert(List<EmployeeBean> list, Integer outputMode) { ArrayList<EmpBeanPub> list2 = new ArrayList<>(); Iterator<EmployeeBean> iter = list == null ? null : list.iterator(); while (iter != null && iter.hasNext()) { EmployeeBean bean1 = iter.next(); EmpBeanPub bean2 = bean1.convertTo(); list2.add(bean2); } return (list2); } private Integer DEFAULT_PAGE_SIZE = 10; }