Exemplo n.º 1
0
  /**
   * *********************************************************** Create
   * ***********************************************************
   */
  @SuppressWarnings("rawtypes")
  private void onNodeTypeChanged() {
    ListModelList typeModel = (ListModelList) typeBox.getModel();
    Set selections = typeModel.getSelection();
    if (selections.isEmpty()) return;
    String selection = (String) selections.iterator().next();
    logger.info("Node type set to: " + selection);

    Map<String, String> ntd = GeneralUtil.getNodeDescriptionByLabel(ntds, selection);
    String nodeType = ntd.get("type");
    if (nodeType.equals(Constants.NODE_TYPE_USER)) {
      firstNameRow.setVisible(true);
      firstNameBox.setConstraint("no empty");
      lastNameRow.setVisible(true);
      lastNameBox.setConstraint("no empty");
      midNameRow.setVisible(true);
    } else {
      firstNameRow.setVisible(false);
      firstNameBox.setConstraint("");
      lastNameRow.setVisible(false);
      lastNameBox.setConstraint("");
      midNameRow.setVisible(false);
    }
  }
Exemplo n.º 2
0
  /**
   * *************************************************** Helper methods
   * ***************************************************
   */
  private Node createNode() throws InterruptedException {
    Comboitem item = typeBox.getSelectedItem();
    if (item == null) {
      Messagebox.show("Node type is required.");
      return null;
    }

    String nodeType = GeneralUtil.getNodeDescriptionByLabel(ntds, item.getLabel()).get("type");
    String firstName = firstNameBox.getValue().trim();
    String lastName = lastNameBox.getValue().trim();
    String midName = midNameBox.getValue().trim();
    String label = labelBox.getValue().trim();
    String username;
    if (nodeType.equals(Constants.NODE_TYPE_USER)) {
      if (firstName.isEmpty()) {
        Messagebox.show("First Name is required.");
        return null;
      } else if (firstName.length() > 80) {
        Messagebox.show("First Name cannot be longer than 80.");
        return null;
      } else if (GeneralUtil.containSpecialCharacter(firstName)) {
        Messagebox.show("First Name cannot contain special characters.");
        return null;
      }

      if (lastName.isEmpty()) {
        Messagebox.show("Last Name is required.");
        return null;
      } else if (lastName.length() > 80) {
        Messagebox.show("Last Name cannot be longer than 80.");
        return null;
      } else if (GeneralUtil.containSpecialCharacter(lastName)) {
        Messagebox.show("Last Name cannot contain special characters.");
        return null;
      }

      username = firstName.replace(" ", "_") + "__" + lastName.replace(" ", "_");
    } else {
      if (label.isEmpty()) {
        Messagebox.show("Label is required.");
        return null;
      } else if (label.length() > 255) {
        Messagebox.show("Label cannot be longer than 255.");
        return null;
      }
      username = GeneralUtil.replaceSpecialCharacter(label, "_").replace(" ", "_");
    }

    if (nodeDao.findByUsername(username) != null) {
      Messagebox.show("Username: "******" has already been used.");
      return null;
    }

    Survey survey = surveyDao.findById(1L);
    Node node = new Node();
    node.setType(nodeType);
    node.setUsername(username);
    node.setLabel(label);
    if (nodeType.equals(Constants.NODE_TYPE_USER)) {
      node.setFirstName(firstName);
      node.setLastName(lastName);
      node.setMidName(midName);
    }

    String defaultPassword = survey.getAttribute(Constants.SURVEY_DEFAULT_PASSWORD);
    if (defaultPassword == null) {
      defaultPassword = "******";
    }
    if (defaultPassword.equals("rAnDoM")) {
      RandomString rs = new RandomString(8);
      defaultPassword = rs.nextString();
    }
    node.setPassword(defaultPassword);

    node.setAttribute(
        Constants.NODE_LOGIN_MODE, survey.getAttribute(Constants.SURVEY_DEFAULT_LOGIN_MODE));

    // role
    node.getRoles().add(roleDao.findByName(Constants.ROLE_USER));

    // groups
    Set<Group> groups = new HashSet<Group>();
    AbstractQuestionRelation p = (AbstractQuestionRelation) getParent();
    groups.addAll(p.getQuestion().getAvailableGroups());
    groups.add(groupDao.findByName(Constants.GROUP_ALL));

    if (nodeType.equals(Constants.NODE_TYPE_USER)) {
      groups.add(groupDao.findByName(Constants.GROUP_USER));
    } else {
      String groupName = Constants.GROUP_NODE_TYPE_PREFIX + nodeType;
      Group group = groupDao.findByName(groupName);
      if (group == null) {
        group = new Group();
        group.setName(groupName);
        groupDao.save(group);
        logger.info("created new group: " + group.getName());
      }
      groups.add(group);
    }

    node.setGroups(groups);

    // save
    nodeDao.save(node);
    logger.debug("Node created: " + username);

    return node;
  }