Exemplo n.º 1
0
  /**
   * Initialises this MultiBulkMoveBean given a list of issues.
   *
   * <p>If this MultiBulkMoveBean links a BulkEditBean with parent issues to BulkEditBeans with
   * subtasks, then include the parent BulkEditBean in the parentBulkEditBean parameter. Otherwise
   * you can pass null.
   *
   * @param issues Issues for this MultiBulkMoveBean.
   * @param parentBulkEditBean If this MultiBulkMoveBean represents subtasks, then this is the
   *     BulkEditBean that contains the parents of the subtasks, otherwise null.
   */
  public void initFromIssues(List issues, BulkEditBean parentBulkEditBean) {
    // Ensure that the order is kept
    issuesInContext = (ListOrderedMap) ListOrderedMap.decorate(new MultiHashMap());
    regularIssues = new ListOrderedMap();
    subTaskIssues = new ArrayList<Issue>();

    // First pass stores att the
    for (final Object issue2 : issues) {
      MutableIssue issue = (MutableIssue) issue2;
      if (!issue.isSubTask()) {
        regularIssues.put(issue.getId(), issue);
      } else {
        subTaskIssues.add(issue);
      }
    }

    // Split it up by context, also check special rule that you can't move sub tasks & its parent
    // all in the same go
    for (final Object issue1 : issues) {
      MutableIssue issue = (MutableIssue) issue1;
      // NOTE: we only do this for the bulk move operation, this is likely the correct behavior for
      // the
      // bulk move operation but I am certain that it is not correct for the bulk migrate operation
      // JRA-10244.
      // In bulk move the wizard will prompt the user with subtask information once it has collected
      // the project
      // information about the parent issues, this is not need in the the issue type scheme
      // migration since you
      // will never be changing the project
      // TODO: Why test for operation name?
      if (BulkMigrateOperation.OPERATION_NAME.equals(operationName)
          && issue.isSubTask()
          && regularIssues.containsKey(issue.getParentId())) {
        log.info(
            "Sub issue: "
                + issue.getKey()
                + " : discarded since parent was also present in the bulk move");
        subTasksDiscarded++;
      } else {
        issuesInContext.put(
            new IssueContextImpl(issue.getProjectObject(), issue.getIssueTypeObject()), issue);
      }
    }

    // Set the bulk edit bean.. sort the keys by project
    bulkEditBeans = new ListOrderedMap();
    List keys = new ArrayList(issuesInContext.keySet());
    Collections.sort(keys);

    for (final Object key : keys) {
      IssueContext context = (IssueContext) key;
      Collection issuesForContext = (Collection) issuesInContext.get(context);

      BulkEditBean bulkEditBean = new BulkEditBeanImpl(issueManager);
      bulkEditBean.initSelectedIssues(issuesForContext);
      bulkEditBean.setOperationName(operationName);
      bulkEditBean.setTargetProject(context.getProjectObject());
      bulkEditBean.setTargetIssueTypeId(
          context.getIssueTypeObject() != null ? context.getIssueTypeObject().getId() : null);
      // Set the Parent BulkEditBean - used by subtask BulkEditBean's to get to the new version of
      // the subtask's parents.
      bulkEditBean.setParentBulkEditBean(parentBulkEditBean);

      bulkEditBeans.put(context, bulkEditBean);
    }
  }
  @Override
  public Map<String, Object> getVelocityParameters(
      Issue issue, CustomField field, FieldLayoutItem fieldLayoutItem) {
    Map<String, Object> params = super.getVelocityParameters(issue, field, fieldLayoutItem);
    params.put("i18n", getI18nBean());
    params.put("baseUrl", applicationProperties.getBaseUrl());

    Long prId;
    if (field.isAllProjects()) {
      prId = Consts.PROJECT_ID_FOR_GLOBAL_CF;
    } else {
      if (issue == null) {
        return params;
      }
      prId = issue.getProjectObject().getId();
    }

    String jqlData = qfMgr.getQueryFieldData(field.getIdAsLong(), prId);
    boolean addNull = qfMgr.getAddNull(field.getIdAsLong(), prId);
    boolean isAutocompleteView = qfMgr.isAutocompleteView(field.getIdAsLong(), prId);
    List<String> options = qfMgr.getLinkeFieldsOptions(field.getIdAsLong(), prId);

    params.put("isAutocompleteView", isAutocompleteView);
    params.put("prId", prId.toString());

    String cfValue = field.getValueFromIssue(issue);
    if (Utils.isValidStr(cfValue)) {
      MutableIssue mi = issueMgr.getIssueObject(cfValue);
      if (mi != null && Utils.isValidStr(mi.getSummary())) {
        StringBuilder sb = new StringBuilder();
        if (options.contains("status")) {
          sb.append(getI18nBean().getText("queryfields.opt.status"))
              .append(": ")
              .append(mi.getStatusObject().getName());
        }
        if (options.contains("assignee") && mi.getAssigneeUser() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          User aUser = mi.getAssigneeUser();
          String encodedUser;
          try {
            encodedUser = URLEncoder.encode(aUser.getName(), "UTF-8");
          } catch (UnsupportedEncodingException e) {
            // --> impossible
            encodedUser = aUser.getName();
          }

          sb.append(getI18nBean().getText("queryfields.opt.assignee"))
              .append(": ")
              .append("<a class='user-hover' rel='")
              .append(aUser.getName())
              .append("' id='issue_summary_assignee_'")
              .append(aUser.getName())
              .append("' href='/secure/ViewProfile.jspa?name='")
              .append(encodedUser)
              .append("'>")
              .append(aUser.getDisplayName())
              .append("</a>");
        }
        if (options.contains("priority") && mi.getPriorityObject() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          sb.append(getI18nBean().getText("queryfields.opt.priority"))
              .append(": ")
              .append(mi.getPriorityObject().getName());
        }
        if (options.contains("due") && mi.getDueDate() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          sb.append(getI18nBean().getText("queryfields.opt.due"))
              .append(": ")
              .append(
                  ComponentAccessor.getJiraAuthenticationContext()
                      .getOutlookDate()
                      .format(mi.getDueDate()));
        }

        if (sb.length() > 0) {
          sb.insert(0, " (");
          sb.append(")");
        }

        IssueData issueData;
        if (options.contains("justDesc")) {
          String descr = mi.getDescription();
          if (Utils.isValidStr(descr)) {
            issueData = new IssueData(descr, sb.toString());
          } else {
            issueData = new IssueData(mi.getSummary(), sb.toString());
          }
        } else if (options.contains("key")) {
          issueData = new IssueData(mi.getKey().concat(":").concat(mi.getSummary()), sb.toString());
        } else {
          issueData = new IssueData(mi.getSummary(), sb.toString());
        }
        params.put("fullValue", issueData);
      }
    }

    if (!Utils.isValidStr(jqlData)) {
      params.put("jqlNotSet", Boolean.TRUE);
      return params;
    }
    params.put("jqlNotSet", Boolean.FALSE);
    params.put("options", options);

    if (options.contains("editKey")) {
      params.put("hasKey", Boolean.TRUE);
    }

    User user = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();
    SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlData);
    if (parseResult.isValid()) {
      params.put("jqlNotValid", Boolean.FALSE);
      Query query = parseResult.getQuery();
      try {
        Map<String, String> cfVals = new LinkedHashMap<String, String>();
        SearchResults results = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
        List<Issue> issues = results.getIssues();
        for (Issue i : issues) {
          String summary;
          if (options.contains("justDesc")) {
            String descr = i.getDescription();
            if (Utils.isValidStr(descr)) {
              summary = descr;
            } else {
              summary = i.getSummary();
            }
          } else if (options.contains("editKey")) {
            summary = i.getKey().concat(":").concat(i.getSummary());
          } else {
            summary = i.getSummary();
          }
          cfVals.put(i.getKey(), summary);
        }

        if (addNull) {
          cfVals.put("Empty", Consts.EMPTY_VALUE);
        }

        String selected = Consts.EMPTY_VALUE;
        String value = (String) issue.getCustomFieldValue(field);
        for (Map.Entry<String, String> cf : cfVals.entrySet()) {
          if (value != null && cf.getKey().equals(value)) {
            selected = value;
            break;
          }
        }

        if (isAutocompleteView) {
          Issue selectedIssue = issueMgr.getIssueObject(selected);
          if (selectedIssue != null) {
            params.put("selIssue", selectedIssue);
          }
        } else {
          if (selected.equals("")) {
            String defaultValue = (String) field.getDefaultValue(issue);
            if (defaultValue != null
                && defaultValue.length() > 0
                && cfVals.keySet().contains(defaultValue)) {
              selected = defaultValue;
            }
          }

          if (cfVals != null && !cfVals.isEmpty() && selected.equals("")) {
            selected = cfVals.keySet().iterator().next();
          }
        }

        params.put("selected", selected);
        params.put("isError", Boolean.FALSE);
        params.put("cfVals", cfVals);
      } catch (SearchException e) {
        params.put("isError", Boolean.TRUE);
      }
    } else {
      params.put("jqlNotValid", Boolean.TRUE);
      return params;
    }

    return params;
  }