@Override public boolean handleMessage(Message message, MessageHandlerContext context) throws MessagingException { log.debug("CreateIssueHandler.handleMessage"); if (!canHandleMessage(message, context.getMonitor())) { return deleteEmail; } try { // get either the sender of the message, or the default reporter User reporter = getReporter(message, context); // no reporter - so reject the message if (reporter == null) { final String error = getI18nBean().getText("admin.mail.no.default.reporter"); context.getMonitor().warning(error); context.getMonitor().messageRejected(message, error); return false; } final Project project = getProject(message); log.debug("Project = " + project); if (project == null) { final String text = getI18nBean().getText("admin.mail.no.project.configured"); context.getMonitor().warning(text); context.getMonitor().messageRejected(message, text); return false; } // Check that the license is valid before allowing issues to be created // This checks for: evaluation licenses expired, user limit licenses where limit has been // exceeded ErrorCollection errorCollection = new SimpleErrorCollection(); // Note: want English locale here for logging purposes I18nHelper i18nHelper = new I18nBean(Locale.ENGLISH); getIssueCreationHelperBean().validateLicense(errorCollection, i18nHelper); if (errorCollection.hasAnyErrors()) { context .getMonitor() .warning( getI18nBean() .getText( "admin.mail.bad.license", errorCollection.getErrorMessages().toString())); return false; } // If user does not have create permissions, there's no point proceeding. Error out here to // avoid a stack // trace blow up from the WorkflowManager later on. if (!getPermissionManager().hasPermission(Permissions.CREATE_ISSUE, project, reporter, true) && reporter.getDirectoryId() != -1) { final String error = getI18nBean().getText("admin.mail.no.create.permission", reporter.getName()); context.getMonitor().warning(error); context.getMonitor().messageRejected(message, error); return false; } log.debug("Issue Type Key = = " + issueType); if (!hasValidIssueType()) { context.getMonitor().warning(getI18nBean().getText("admin.mail.invalid.issue.type")); return false; } String summary = message.getSubject(); if (!TextUtils.stringSet(summary)) { context.getMonitor().error(getI18nBean().getText("admin.mail.no.subject")); return false; } if (summary.length() > SummarySystemField.MAX_LEN.intValue()) { context.getMonitor().info("Truncating summary field because it is too long: " + summary); summary = summary.substring(0, SummarySystemField.MAX_LEN.intValue() - 3) + "..."; } // JRA-7646 - check if priority/description is hidden - if so, do not set String priority = null; String description = null; if (!getFieldVisibilityManager() .isFieldHiddenInAllSchemes( project.getId(), IssueFieldConstants.PRIORITY, Collections.singletonList(issueType))) { priority = getPriority(message); } if (!getFieldVisibilityManager() .isFieldHiddenInAllSchemes( project.getId(), IssueFieldConstants.DESCRIPTION, Collections.singletonList(issueType))) { description = getDescription(reporter, message); } MutableIssue issueObject = getIssueFactory().getIssue(); issueObject.setProjectObject(project); issueObject.setSummary(summary); issueObject.setDescription(description); issueObject.setIssueTypeId(issueType); issueObject.setReporter(reporter); // if no valid assignee found, attempt to assign to default assignee User assignee = null; if (ccAssignee) { assignee = getFirstValidAssignee(message.getAllRecipients(), project); } if (assignee == null) { assignee = getAssigneeResolver().getDefaultAssignee(issueObject, Collections.EMPTY_MAP); } if (assignee != null) { issueObject.setAssignee(assignee); } issueObject.setPriorityId(priority); // Ensure issue level security is correct setDefaultSecurityLevel(issueObject); /* * + FIXME -- set cf defaults @todo + */ // set default custom field values // CustomFieldValuesHolder cfvh = new CustomFieldValuesHolder(issueType, project.getId()); // fields.put("customFields", CustomFieldUtils.getCustomFieldValues(cfvh.getCustomFields())); Map<String, Object> fields = new HashMap<String, Object>(); fields.put("issue", issueObject); // TODO: How is this supposed to work? There is no issue created yet; ID = null. // wseliga note: Ineed I think that such call does not make sense - it will be always null MutableIssue originalIssue = getIssueManager().getIssueObject(issueObject.getId()); // Give the CustomFields a chance to set their default values JRA-11762 List<CustomField> customFieldObjects = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects(issueObject); for (CustomField customField : customFieldObjects) { issueObject.setCustomFieldValue(customField, customField.getDefaultValue(issueObject)); } fields.put(WorkflowFunctionUtils.ORIGINAL_ISSUE_KEY, originalIssue); final Issue issue = context.createIssue(reporter, issueObject); if (issue != null) { // Add Cc'ed users as watchers if params set - JRA-9983 if (ccWatcher) { addCcWatchersToIssue(message, issue, reporter, context, context.getMonitor()); } // Record the message id of this e-mail message so we can track replies to this message // and associate them with this issue recordMessageId( MailThreadManager.ISSUE_CREATED_FROM_EMAIL, message, issue.getId(), context); } // TODO: if this throws an error, then the issue is already created, but the email not deleted // - we will keep "handling" this email over and over :( createAttachmentsForMessage(message, issue, context); return true; } catch (Exception e) { context.getMonitor().warning(getI18nBean().getText("admin.mail.unable.to.create.issue"), e); } // something went wrong - don't delete the message return false; }
@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; }