Java tutorial
package com.jiangyifen.ec2.ui.mgr.usermanage; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; import com.jiangyifen.ec2.bean.RoleType; import com.jiangyifen.ec2.entity.Department; import com.jiangyifen.ec2.entity.Domain; import com.jiangyifen.ec2.entity.Phone2PhoneSetting; import com.jiangyifen.ec2.entity.Queue; import com.jiangyifen.ec2.entity.Role; import com.jiangyifen.ec2.entity.User; import com.jiangyifen.ec2.entity.UserQueue; import com.jiangyifen.ec2.globaldata.ShareData; import com.jiangyifen.ec2.service.csr.ami.QueueMemberRelationService; import com.jiangyifen.ec2.service.eaoservice.DepartmentService; import com.jiangyifen.ec2.service.eaoservice.Phone2PhoneSettingService; import com.jiangyifen.ec2.service.eaoservice.QueueService; import com.jiangyifen.ec2.service.eaoservice.RoleService; import com.jiangyifen.ec2.service.eaoservice.UserQueueService; import com.jiangyifen.ec2.service.eaoservice.UserService; import com.jiangyifen.ec2.ui.mgr.system.tabsheet.UserManagement; import com.jiangyifen.ec2.ui.mgr.util.StyleConfig; import com.jiangyifen.ec2.utils.SpringContextHolder; import com.vaadin.data.Item; import com.vaadin.data.util.BeanItem; import com.vaadin.data.util.BeanItemContainer; import com.vaadin.data.validator.RegexpValidator; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.ComboBox; import com.vaadin.ui.Component; import com.vaadin.ui.DefaultFieldFactory; import com.vaadin.ui.Field; import com.vaadin.ui.Form; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.OptionGroup; import com.vaadin.ui.TextField; import com.vaadin.ui.TwinColSelect; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; /** *? * @author chb */ @SuppressWarnings("serial") public class EditUser extends Window implements Button.ClickListener { private static final Object[] USER_COL_ORDER = new Object[] { "empNo", "realName", "username", "password", "age", "gender", "phoneNumber", "emailAddress", "department", "roles" }; // ? private static final String[] USER_COL_NAME = new String[] { " ?", "??", " ??", "?", "", "", "???", "?", "", "" }; //?Field?? /** * ? */ //Form private Form form; //?? private Button save; private Button cancel; /** * */ //?User private UserManagement userManagement; private User user; private User loginUser; private Domain domain; // ? private String originalPhoneNum; // ?? private List<Role> allRoles; private String originalEmpNo; //?? private String originalUsername; //??? private UserService userService; private DepartmentService departmentService; private RoleService roleService; private QueueService queueService; private QueueMemberRelationService queueMemberRelationService; // ??? private Phone2PhoneSettingService phone2PhoneSettingService; // ?? private UserQueueService userQueueService; // ??? /** * User * @param project */ public EditUser(UserManagement userManagement) { this.center(); this.setModal(true); this.userManagement = userManagement; domain = SpringContextHolder.getDomain(); loginUser = SpringContextHolder.getLoginUser(); //Service? userService = SpringContextHolder.getBean("userService"); departmentService = SpringContextHolder.getBean("departmentService"); roleService = SpringContextHolder.getBean("roleService"); queueService = SpringContextHolder.getBean("queueService"); queueMemberRelationService = SpringContextHolder.getBean("queueMemberRelationService"); phone2PhoneSettingService = SpringContextHolder.getBean("phone2PhoneSettingService"); userQueueService = SpringContextHolder.getBean("userQueueService"); //WindowLayout VerticalLayout windowContent = new VerticalLayout(); windowContent.setSizeUndefined(); windowContent.setMargin(false, true, true, true); windowContent.setSpacing(true); windowContent.setStyleName(StyleConfig.VERTICAL_STYLE); this.setContent(windowContent); //From form = new Form(); form.setValidationVisibleOnCommit(true); form.setValidationVisible(false); form.addStyleName("chb"); //Form????? allRoles = roleService.getAll(domain); form.setFormFieldFactory(new MyFieldFactory()); form.setFooter(buildButtonsLayout()); windowContent.addComponent(form); } /** * * @return */ private HorizontalLayout buildButtonsLayout() { HorizontalLayout buttonsLayout = new HorizontalLayout(); buttonsLayout.setSpacing(true); // ? save = new Button("?"); save.setStyleName("default"); save.addListener(this); buttonsLayout.addComponent(save); //? cancel = new Button("?"); cancel.addListener(this); buttonsLayout.addComponent(cancel); return buttonsLayout; } /** * attach ???form? */ @SuppressWarnings("unchecked") private void updateFormInfo() { this.setCaption(""); //Form?? form.setItemDataSource(new BeanItem<User>(user), Arrays.asList(USER_COL_ORDER)); for (int i = 0; i < USER_COL_ORDER.length; i++) { form.getField(USER_COL_ORDER[i]).setCaption(USER_COL_NAME[i]); } // ComboBox depts = (ComboBox) form.getField("department"); Collection<Department> deptSource = (Collection<Department>) depts.getContainerDataSource().getItemIds(); for (Department dept : deptSource) { if (dept.getId().equals(user.getDepartment().getId())) { form.getField("department").setValue(dept); break; } } //?? Set<Role> selectedRoles = user.getRoles(); Set<Role> roleSelectValue = new HashSet<Role>(); for (Role role : selectedRoles) { for (int i = 0; i < allRoles.size(); i++) { if (role.getId().equals(allRoles.get(i).getId())) { roleSelectValue.add(allRoles.get(i)); } } } form.getField("roles").setValue(roleSelectValue); } /** * buttonClick ? */ private boolean executeSave() { try { //form.commit(); ??commit user.setRegistedDate(new Date()); user.setDomain(SpringContextHolder.getDomain()); user = userService.update(user); //userManagementTable? userManagement.refreshTable(false); } catch (Exception e) { e.printStackTrace(); this.getApplication().getMainWindow().showNotification( "????", Notification.TYPE_WARNING_MESSAGE); return false; } return true; } /** * */ private class MyFieldFactory extends DefaultFieldFactory { private String mailRegex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; @Override public Field createField(Item item, Object propertyId, Component uiContext) { if ("empNo".equals(propertyId)) { TextField empNoField = new TextField(); empNoField.setRequired(true); empNoField.setWidth("180px"); empNoField.setNullRepresentation(""); empNoField.setNullSettingAllowed(true); empNoField.setRequiredError("???"); empNoField.addValidator( new RegexpValidator("\\d{1,32}", "???32??")); return empNoField; } else if ("password".equals(propertyId)) { TextField passwordField = new TextField(); passwordField.setNullRepresentation(""); passwordField.setRequired(true); passwordField.setWidth("180px"); passwordField.setRequiredError("??"); passwordField.addValidator( new RegexpValidator("\\w{0,32}", "???32??")); return passwordField; } else if ("username".equals(propertyId)) { TextField username = new TextField(); username.setNullRepresentation(""); username.setRequired(true); username.setWidth("180px"); username.setRequiredError("???"); username.addValidator(new RegexpValidator("\\w{0,64}", "????64??")); return username; } else if ("gender".equals(propertyId)) { OptionGroup optionGroup = new OptionGroup("", Arrays.asList("", "")); optionGroup.setStyleName("twocolchb"); return optionGroup; } else if ("roles".equals(propertyId)) { TwinColSelect roleSelect = new TwinColSelect(); roleSelect.setItemCaptionPropertyId("name"); roleSelect.setLeftColumnCaption(""); roleSelect.setRightColumnCaption(""); roleSelect.setMultiSelect(true); roleSelect.setImmediate(true); roleSelect.setWidth("350px"); roleSelect.setRows(4); roleSelect.setRequired(true); roleSelect.setRequiredError(""); //RoleSelect?? BeanItemContainer<Role> roleContainer = new BeanItemContainer<Role>(Role.class); roleContainer.addAll(allRoles); roleSelect.setContainerDataSource(roleContainer); return roleSelect; } else if ("department".equals(propertyId)) { ComboBox depts = new ComboBox(); BeanItemContainer<Department> deptContainer = new BeanItemContainer<Department>(Department.class); HashMap<Long, Department> departmentMap = new HashMap<Long, Department>(); for (Role role : loginUser.getRoles()) { if (role.getType().equals(RoleType.manager)) { List<Department> departments = departmentService.getGovernedDeptsByRole(role.getId()); for (Department dept : departments) { departmentMap.put(dept.getId(), dept); } } } deptContainer.addAll(departmentMap.values()); // deptContainer.addAll(departmentService.getAll(domain)); depts.setContainerDataSource(deptContainer); depts.setItemCaptionPropertyId("name"); depts.setNullSelectionAllowed(false); depts.setInputPrompt(""); depts.setTextInputAllowed(false); depts.setImmediate(true); depts.setWidth("180px"); depts.setRequired(true); depts.setRequiredError(""); return depts; } Field field = DefaultFieldFactory.get().createField(item, propertyId, uiContext); if ("realName".equals(propertyId)) { field.addValidator(new RegexpValidator("^[2E80-\\u9fa5\\-]{0,64}$", "??64?")); } else if ("age".equals(propertyId)) { field.addValidator( new RegexpValidator("\\d{0,4}", "??4?")); } else if ("phoneNumber".equals(propertyId)) { field.addValidator(new RegexpValidator("\\d{0,32}", "???32??")); } else if ("emailAddress".equals(propertyId)) { field.addValidator(new RegexpValidator(mailRegex, "??")); } if (field instanceof TextField) { ((TextField) field).setNullRepresentation(""); ((TextField) field).setNullSettingAllowed(true); } field.setWidth("180px"); return field; } } /** * attach */ @Override public void attach() { super.attach(); user = (User) userManagement.getTable().getValue(); originalEmpNo = user.getEmpNo(); originalUsername = user.getUsername(); originalPhoneNum = user.getPhoneNumber(); updateFormInfo(); } /** * ??? * @param event */ @Override public void buttonClick(ClickEvent event) { Button source = event.getButton(); if (source == save) { Domain domain = SpringContextHolder.getDomain(); //? String empNo = ""; String currentphoneNum = ""; try { Object obj1 = form.getField("empNo").getValue(); if (obj1 != null) { empNo = obj1.toString(); } //?? String username = ""; Object obj2 = form.getField("username").getValue(); if (obj2 != null) { username = obj2.toString(); } // jrh ???? Object obj3 = form.getField("phoneNumber").getValue(); if (obj3 != null) { currentphoneNum = obj3.toString(); } //???Csr?? List<User> sameEmpNoCsrs = userService.getUsersByEmpNo(empNo, domain); List<User> sameUsernameCsrs = userService.getUsersByUsername(username); //? if ((!originalEmpNo.equals(empNo)) && sameEmpNoCsrs.size() > 0) { this.showNotification("?????"); return; } //?? if ((!originalUsername.equals(username)) && sameUsernameCsrs.size() > 0) { this.showNotification("???????"); return; } form.commit(); } catch (Exception e) { return; } boolean saveSuccess = executeSave(); if (saveSuccess) { // ????? ShareData domainToEmpNos if (!originalEmpNo.equals(empNo)) { List<String> empNos = new ArrayList<String>(); for (User user : userService.getAllByDomain(domain)) { empNos.add(user.getEmpNo()); } } // jrh ??????? updatePhoneInQueueMember(originalPhoneNum, currentphoneNum); this.getParent().removeWindow(this); } } else if (source == cancel) { form.discard(); this.getParent().removeWindow(this); } } /** * jrh * ??????? * @param originalPhoneNum ??? * @param currentPhoneNum ???? */ private void updatePhoneInQueueMember(String originalPhoneNum, String currentphoneNum) { String defaultOutline = ShareData.domainToDefaultOutline.get(domain.getId()); ; if (defaultOutline == null) { return; // ??? } // ? Phone2PhoneSetting globalSetting = phone2PhoneSettingService.getGlobalSettingByDomain(domain.getId()); // ?? if (globalSetting == null) { return; } // ?? List<Queue> allCommonQueues = queueService.getAllByDomain(domain, true); List<String> allCommonQueueNames = new ArrayList<String>(); for (Queue autoQueue : allCommonQueues) { allCommonQueueNames.add(autoQueue.getName()); } // ??? 1????2??????(??) boolean isGlobalRunning = phone2PhoneSettingService.confirmSettingIsRunning(globalSetting); if (isGlobalRunning) { // ????? if (globalSetting.getIsSpecifiedPhones()) { return; } // ???????????? for (User specifiedCsr : globalSetting.getSpecifiedCsrs()) { if (specifiedCsr.getId() == user.getId()) { updateSpecifyCsrQueueMember(specifiedCsr, originalPhoneNum, currentphoneNum, defaultOutline, allCommonQueueNames); return; } } } // ??(??????) // ?? 1??????2??????? if (!globalSetting.getIsSpecifiedPhones()) { for (User specifiedCsr : globalSetting.getSpecifiedCsrs()) { // ????[???] if (specifiedCsr.getId() == user.getId()) { return; } } } // 3????4???5?? <= ? if (globalSetting.getIsLicensed2Csr()) { Phone2PhoneSetting customSetting = phone2PhoneSettingService.getByUser(user.getId()); if (customSetting != null) { boolean isCustomRunning = phone2PhoneSettingService.confirmSettingIsRunning(customSetting); if (isCustomRunning) { updateSpecifyCsrQueueMember(customSetting.getCreator(), originalPhoneNum, currentphoneNum, defaultOutline, allCommonQueueNames); } } } } /** * jrh * ????????? * @param csr ? * @param originalPhoneNum ??? * @param currentPhoneNum ???? * @param outlineName * @param allCommonQueues ? */ private void updateSpecifyCsrQueueMember(User csr, String originalPhoneNum, String currentPhoneNum, String outlineName, List<String> allCommonQueues) { // ???? if (ShareData.userToExten.keySet().contains(csr.getId())) { return; } // ??????? List<UserQueue> userQueues = userQueueService.getAllByUsername(csr.getUsername()); for (UserQueue userQueue : userQueues) { String queueName = userQueue.getQueueName(); if (!allCommonQueues.contains(queueName)) { // ????? continue; } if (originalPhoneNum != null && !"".equals(originalPhoneNum)) { queueMemberRelationService.removeQueueMemberRelation(queueName, originalPhoneNum + "@" + outlineName); } if (currentPhoneNum != null && !"".equals(currentPhoneNum)) { queueMemberRelationService.addQueueMemberRelation(queueName, currentPhoneNum + "@" + outlineName, 5); } } } }