Java tutorial
package com.tms.controller.customer; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.tms.constant.Constants; import com.tms.controller.base.BaseController; import com.tms.entity.Company; import com.tms.entity.CompanyStatus; import com.tms.entity.CompanyStructure; import com.tms.entity.CompanyStructureLevel; import com.tms.entity.Employee; import com.tms.entity.Role; import com.tms.entity.User; import com.tms.entity.UserSource; import com.tms.entity.UserStatus; import com.tms.entity.customer.Features; import com.tms.entity.customer.OutFaceType; import com.tms.entity.customer.ReportManagement; import com.tms.entity.customer.TimeZone; import com.tms.service.company.CompanyService; import com.tms.service.features.FeaturesService; import com.tms.service.outfacetype.OutFaceTypeService; import com.tms.service.reportmanagement.ReportManagementService; import com.tms.service.role.RoleService; import com.tms.service.timezone.TimeZoneService; import com.tms.service.tms.TMSService; import com.tms.service.user.UserService; import com.tms.utils.MD5; import com.tms.utils.SessionUtils; import com.tms.utils.ThreadUtils; import com.tms.utils.email.EmailServer; import com.tms.vo.CompanyVo; @RequestMapping("/customer") @Controller public class CustomerController extends BaseController<Object> { @Autowired private CompanyService companyService; @Autowired private UserService userService; @Autowired private TimeZoneService timeZoneService; @Autowired private OutFaceTypeService outFaceTypeService; @Autowired private ReportManagementService reportManagementService; @Autowired private FeaturesService featuresService; @Autowired private TMSService tmsService; @Autowired private RoleService roleService; @RequestMapping("/list") public ModelAndView customerList(String searchStr, String order, String sort, HttpServletResponse response, HttpServletRequest request) throws Exception { ModelAndView view = new ModelAndView(); List<Company> list = companyService.findAll(); view.getModelMap().put("customers", list); view.setViewName("admin/customer/customerlist"); return view; } @RequestMapping("/list_nd") public ModelAndView customerListValue(HttpServletResponse response, HttpServletRequest request) throws Exception { ModelAndView view = new ModelAndView(); List<Company> list = companyService.findAll(); view.getModelMap().put("customers", list); view.setViewName("admin/customer/customerlisttable"); return view; } @RequestMapping("/create") public ModelAndView createCustomer(HttpServletResponse response, HttpServletRequest request) throws Exception { ModelAndView view = new ModelAndView(); List<TimeZone> timeZones = timeZoneService.findAll();// view.getModelMap().put("timeZones", timeZones); List<OutFaceType> outFaceTypes = outFaceTypeService.findAll();//? List<OutFaceType> outFaceTypeArrayList = new ArrayList<OutFaceType>(); List<OutFaceType> otherOutFaceTypeArrayList = new ArrayList<OutFaceType>(); if (CollectionUtils.isNotEmpty(outFaceTypes)) { for (OutFaceType outfaceType : outFaceTypes) { if (outfaceType.isOther()) { otherOutFaceTypeArrayList.add(outfaceType); } else { outFaceTypeArrayList.add(outfaceType); } } } view.getModelMap().put("outFace", outFaceTypeArrayList); view.getModelMap().put("otherOutFace", otherOutFaceTypeArrayList); List<ReportManagement> reportManagements = reportManagementService.findAll();//? view.getModelMap().put("reportManagements", reportManagements); List<Features> features = featuresService.findAll();//? view.getModelMap().put("features", features); view.getModelMap().put("customerStatus", CompanyStatus.values()); view.setViewName("admin/customer/savecustomer"); return view; } @RequestMapping("/update") public ModelAndView updateCustomer(String id, HttpServletResponse response, HttpServletRequest request) throws Exception { ModelAndView view = new ModelAndView(); Company customer = companyService.findById(id); view.getModel().put("customer", customer); String[] customerStatus = new String[CompanyStatus.values().length]; int index = 0; for (CompanyStatus companyStatus : CompanyStatus.values()) {//? customerStatus[index] = companyStatus.name(); index++; } view.getModelMap().put("customerStatus", customerStatus); List<TimeZone> timeZones = timeZoneService.findAll(); view.getModelMap().put("timeZones", timeZones); List<OutFaceType> outFaceTypes = outFaceTypeService.findAll(); if (CollectionUtils.isNotEmpty(outFaceTypes)) { List<OutFaceType> outFaceTypeArrayList = new ArrayList<OutFaceType>(); List<OutFaceType> otherOutFaceTypeArrayList = new ArrayList<OutFaceType>(); for (OutFaceType outfaceType : outFaceTypes) { if (outfaceType.isOther()) { otherOutFaceTypeArrayList.add(outfaceType); } else { outFaceTypeArrayList.add(outfaceType); } } view.getModelMap().put("outFace", outFaceTypeArrayList); view.getModelMap().put("otherOutFace", otherOutFaceTypeArrayList); } List<ReportManagement> reportManagements = reportManagementService.findAll(); view.getModelMap().put("reportManagements", reportManagements); List<Features> features = featuresService.findAll(); view.getModelMap().put("features", features); if (customer.getOutFaceTypes() != null) { for (OutFaceType outfaceType : customer.getOutFaceTypes()) { if (outfaceType.isOther()) { view.getModelMap().put("otherOutFaceTypeId", outfaceType.getId()); } else { view.getModelMap().put("outFaceTypeId", outfaceType.getId()); } } } if (customer.getStatus() != null) { view.getModelMap().put("customerStatus", customer.getStatus().name()); } view.setViewName("admin/customer/savecustomer"); return view; } @RequestMapping("/save") public ModelAndView saveCustomer(CompanyVo customerVo, HttpServletResponse response, HttpServletRequest request) throws Exception { ModelAndView view = new ModelAndView(); try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Company customer = new Company(); if (StringUtils.isNotEmpty(customerVo.getId())) { customer = companyService.findById(customerVo.getId()); } else { if (companyService.validateNumberExist(customerVo.getNumber())) { view.getModel().put("message", "existNo"); return view; } if (companyService.validateEmailExist(customerVo.getEmail())) { view.getModel().put("message", "existEmail"); return view; } if (userService.validateEmailExist(customer.getEmail())) {//user email??? view.getModel().put("message", "existEmail"); return view; } } if (StringUtils.isNotEmpty(customerVo.getNumber())) { customer.setNumber(customerVo.getNumber()); } if (StringUtils.isNotEmpty(customerVo.getStatus())) { customer.setStatus(CompanyStatus.valueOf(customerVo.getStatus())); } if (StringUtils.isNotEmpty(customerVo.getTradingName())) { customer.setTradingName(customerVo.getTradingName()); } customer.setPause(customerVo.isPause()); if (StringUtils.isNotEmpty(customerVo.getDomain())) { customer.setDomain(customerVo.getDomain()); } if (StringUtils.isNotEmpty(customerVo.getOpenedOn())) { customer.setOpenedOn(sdf.parse(customerVo.getOpenedOn())); } if (StringUtils.isNotEmpty(customerVo.getClosedOn())) { customer.setClosedOn(sdf.parse(customerVo.getClosedOn())); } customer.setLicenses(customerVo.getLicenses()); customer.setPayrollEnabled(customerVo.isPayrollEnabled()); customer.setTeamEnabled(customerVo.isTeamEnabled()); if (StringUtils.isNotEmpty(customerVo.getToken())) { customer.setToken(customerVo.getToken()); } if (StringUtils.isNotEmpty(customerVo.getName())) { customer.setName(customerVo.getName()); } if (StringUtils.isNotEmpty(customerVo.getTitle())) { customer.setTitle(customerVo.getTitle()); } if (StringUtils.isNotEmpty(customerVo.getEmail())) { customer.setEmail(customerVo.getEmail()); } if (StringUtils.isNotEmpty(customerVo.getMobile())) { customer.setMobile(customerVo.getMobile()); } if (StringUtils.isNotEmpty(customerVo.getPhone())) { customer.setPhone(customerVo.getPhone()); } if (StringUtils.isNotEmpty(customerVo.getFax())) { customer.setFax(customerVo.getFax()); } if (StringUtils.isNotEmpty(customerVo.getPhysicalAddr())) { customer.setPhysicalAddr(customerVo.getPhysicalAddr()); } if (StringUtils.isNotEmpty(customerVo.getPostalAddr())) { customer.setPostalAddr(customerVo.getPostalAddr()); } if (StringUtils.isNotEmpty(customerVo.getDayBegin())) { customer.setDayBegin(customerVo.getDayBegin()); } if (StringUtils.isNotEmpty(customerVo.getDayEnd())) { customer.setDayEnd(customerVo.getDayEnd()); } customer.setPerviousDay(customerVo.isPerviousDay()); customer.setNextDay(customerVo.isNextDay()); if (StringUtils.isNotEmpty(customerVo.getTimeZoneId())) { TimeZone timeZone = timeZoneService.findById(customerVo.getTimeZoneId()); if (timeZone != null) { customer.setTimeZone(timeZone); } } Set<OutFaceType> outFaceTypes = new HashSet<OutFaceType>(); if (StringUtils.isNotEmpty(customerVo.getOutFaceTypeId())) { OutFaceType outFaceType = outFaceTypeService.findById(customerVo.getOutFaceTypeId()); if (outFaceType != null) { outFaceTypes.add(outFaceType); } } if (StringUtils.isNotEmpty(customerVo.getOtherOutFaceTypeId())) { OutFaceType outFaceType = outFaceTypeService.findById(customerVo.getOtherOutFaceTypeId()); if (outFaceType != null) { outFaceTypes.add(outFaceType); } } customer.setOutFaceTypes(outFaceTypes); if (customerVo.getReportManagementIds() != null) { Set<ReportManagement> reportManagements = new HashSet<ReportManagement>(); for (String reportManagementId : customerVo.getReportManagementIds()) { ReportManagement reportManagement = reportManagementService.findById(reportManagementId); if (reportManagement != null) { reportManagements.add(reportManagement); } } customer.setReportManagements(reportManagements); } if (customerVo.getFeaturesIds() != null) { Set<Features> featureses = new HashSet<Features>(); for (String featuresId : customerVo.getFeaturesIds()) { Features features = featuresService.findById(featuresId); if (features != null) { featureses.add(features); } } customer.setFeatures(featureses); } if (StringUtils.isEmpty(customer.getId())) { customer.setCreatedOn(new Date()); customer.setCompanyStructure(getCompanyStructures(customer)); companyService.save(customer); saveUser(customer); } else { companyService.update(customer); } view.getModel().put("message", "success"); } catch (Exception e) { e.printStackTrace(); view.getModel().put("message", "error"); } return view; } /** * ?Position? * @param customer * @return */ private Set<CompanyStructure> getCompanyStructures(Company customer) { Set<CompanyStructure> companyStructures = new HashSet<>(); for (CompanyStructureLevel companyStructureLevel : CompanyStructureLevel.values()) { CompanyStructure companyStructure = new CompanyStructure(); companyStructure.setCompany(customer); if (companyStructureLevel == CompanyStructureLevel.Position) { companyStructure.setIsActive(true); } companyStructure.setLabelName(companyStructureLevel.name()); companyStructure.setLevel(companyStructureLevel); companyStructures.add(companyStructure); } return companyStructures; } private void sendEmail(final String password, final String email) { ThreadUtils.run(new Runnable() { public void run() { EmailServer emailServer = EmailServer.getInstance(); try { emailServer.sendHtmlMail("password", password, email); } catch (Exception e) { e.printStackTrace(); } } }); } @RequestMapping("/binging") public ModelAndView bingingCompany(String id, HttpServletRequest request) { ModelAndView view = new ModelAndView(); User user = SessionUtils.getUser(request); Company company = companyService.findById(id); if (company == null) { view.getModel().put("message", "error"); return view; } user.setCompany(company); view.getModel().put("message", "success"); return view; } private String saveUser(Company customer) { if (customer == null && customer.getEmail() == null) return null; User userInfo = new User(); userInfo.setEmail(customer.getEmail()); Set<Role> roles = new HashSet<Role>(); List<Role> roleList = roleService.findAll(); Role userRole = null; for (Role role : roleList) { if (role.getName().equals(Constants.ADMIN)) { userRole = role; break; } } if (userRole != null) { roles.add(userRole); userInfo.setRoles(roles); Employee employee = new Employee(); //employee.setStatus(EmployeeStatus.Active);// //employee.setIsExport(false);//? employee.setDailyHours(0.0); employee.setFirstName(customer.getEmail().substring(0, customer.getEmail().indexOf("@"))); // employee.setLastName(customer.getEmail().substring(0, customer.getEmail().indexOf("@"))); // employee.setMiddleName(customer.getEmail().substring(0, customer.getEmail().indexOf("@"))); employee.setCreateTime(new Date()); employee.setUser(userInfo); userInfo.getEmployee().add(employee); userInfo.setSource(UserSource.User); userInfo.setCreateTime(new Date()); userInfo.setStatus(UserStatus.Active); userInfo.setSys(true); userInfo.setIsEmployee(false);//?employee userInfo.setCompany(customer); userInfo.setPassword(MD5.digest(customer.getEmail().substring(0, customer.getEmail().indexOf("@")))); userService.save(userInfo); sendEmail(customer.getEmail().substring(0, customer.getEmail().indexOf("@")), customer.getEmail()); } return null; } }