/components/server-data-access/src/main/java/org/marvelution/jji/data/services/DefaultIssueReferenceProvider.java
Java | 203 lines | 174 code | 14 blank | 15 comment | 16 complexity | 107faa244d00512c71232de3c2ae8b9b MD5 | raw file
- /*
- * Copyright (c) 2012-present Marvelution Holding B.V.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.marvelution.jji.data.services;
- import java.util.*;
- import java.util.function.*;
- import javax.annotation.*;
- import javax.inject.*;
- import com.marvelution.jji.jira.rest.model.*;
- import org.marvelution.jji.data.services.api.*;
- import org.marvelution.jji.model.*;
- import com.atlassian.jira.issue.Issue;
- import com.atlassian.jira.issue.*;
- import com.atlassian.jira.issue.fields.*;
- import com.atlassian.jira.issue.fields.rest.*;
- import com.atlassian.jira.issue.fields.rest.json.*;
- import com.atlassian.jira.project.Project;
- import com.atlassian.jira.user.*;
- import com.atlassian.plugin.spring.scanner.annotation.imports.*;
- import com.atlassian.plugins.rest.common.json.*;
- import com.google.common.collect.*;
- import com.jayway.jsonpath.*;
- import org.codehaus.jackson.map.Module;
- import org.slf4j.*;
- import static com.marvelution.jji.jira.rest.model.IssueFields.*;
- import static org.marvelution.jji.utils.KeyExtractor.*;
- import static java.util.stream.Collectors.*;
- import static javax.ws.rs.core.UriBuilder.*;
- @Named
- public class DefaultIssueReferenceProvider
- implements IssueReferenceProvider
- {
- private static final Logger LOGGER = LoggerFactory.getLogger(DefaultIssueReferenceProvider.class);
- private static final Map<String, Function<Issue, ApplicationUser>> USER_FIELDS = ImmutableMap.of(CREATOR, Issue::getCreator, ASSIGNEE,
- Issue::getAssignee, REPORTER,
- Issue::getReporter);
- private final ConfigurationService configurationService;
- private final IssueManager issueManager;
- private final FieldManager fieldManager;
- private final JaxbJsonMarshaller jaxbJsonMarshaller;
- @Inject
- public DefaultIssueReferenceProvider(
- ConfigurationService configurationService,
- @ComponentImport IssueManager issueManager,
- @ComponentImport FieldManager fieldManager,
- List<Module> modules)
- {
- this.configurationService = configurationService;
- this.issueManager = issueManager;
- this.fieldManager = fieldManager;
- this.jaxbJsonMarshaller = DefaultJaxbJsonMarshaller.builder().modules(modules).build();
- }
- @Override
- public void setIssueUrl(IssueReference reference)
- {
- reference.setIssueUrl(fromUri(configurationService.getBaseUrl()).path("browse/{issueKey}").build(reference.getIssueKey()));
- }
- @Override
- public Optional<IssueReference> getIssueReference(
- String key,
- FieldContext fieldContext)
- {
- return Optional.of(key).map(issueManager::getIssueObject).map(issue -> toReference(issue, fieldContext));
- }
- @Override
- public Set<IssueReference> getIssueReferences(
- Set<String> keys,
- FieldContext fieldContext)
- {
- return keys.stream()
- .map(issueManager::getIssueObject)
- .filter(Objects::nonNull)
- .map(issue -> toReference(issue, fieldContext))
- .collect(toSet());
- }
- private IssueReference toReference(
- Issue issue,
- FieldContext fieldContext)
- {
- IssueReference reference = new IssueReference().setIssueKey(issue.getKey())
- .setProjectKey(Optional.ofNullable(issue.getProjectObject())
- .map(Project::getKey)
- .orElse(extractProjectKeyFromIssueKey(issue.getKey())));
- setIssueUrl(reference);
- if (fieldContext.hasAdditionalFields())
- {
- for (String fieldKey : fieldContext.additionalFields())
- {
- FieldJsonRepresentation jsonRepresentation = null;
- Field field = fieldManager.getField(fieldKey);
- if (field == null)
- {
- LOGGER.warn("Skipping non-existing field {} for issue {}", fieldKey, issue.getKey());
- continue;
- }
- else if (field instanceof KeySystemField)
- {
- LOGGER.debug("Skipping issuekey field for issue {}", issue.getKey());
- continue;
- }
- else if (USER_FIELDS.containsKey(field.getId()))
- {
- ApplicationUser user = USER_FIELDS.get(field.getId()).apply(issue);
- if (user != null)
- {
- User userJson = new User();
- userJson.setAccountId(String.valueOf(user.getId()));
- userJson.setKey(user.getKey());
- userJson.setName(user.getName());
- userJson.setDisplayName(user.getDisplayName());
- userJson.setEmailAddress(user.getEmailAddress());
- jsonRepresentation = new FieldJsonRepresentation(new JsonData(userJson), null);
- }
- }
- else if (field instanceof RestAwareField)
- {
- jsonRepresentation = ((RestAwareField) field).getJsonFromIssue(issue, true, null);
- }
- if (jsonRepresentation != null)
- {
- String value = getFieldValueFromJson(field.getId(), field.getName(), jsonRepresentation, fieldContext);
- if (value != null)
- {
- reference.addField(field.getName(), value);
- }
- else
- {
- LOGGER.debug("Skipping field {} of issue {} as its value is 'null'", fieldKey, issue.getKey());
- }
- }
- else
- {
- LOGGER.warn("Unable to get value of field {} for issue {}", fieldKey, issue.getKey());
- }
- }
- }
- return reference;
- }
- @Nullable
- protected String getFieldValueFromJson(
- String fieldKey,
- String fieldName,
- FieldJsonRepresentation jsonRepresentation,
- FieldContext fieldContext)
- {
- try
- {
- Object valueJson = getJson(jsonRepresentation.getStandardData());
- Object renderedValueJson = getJson(jsonRepresentation.getRenderedData());
- LOGGER.debug("Attempting to extract the value for field {} from value: {}; renderedValue: {}", fieldKey, valueJson,
- renderedValueJson);
- return fieldContext.fieldValue(fieldKey, fieldName, valueJson, renderedValueJson);
- }
- catch (Exception e)
- {
- LOGGER.error("failed to extract field value for field {}", fieldKey, e);
- return null;
- }
- }
- private Object getJson(JsonData jsonData)
- {
- try
- {
- return Optional.ofNullable(jsonData)
- .map(JsonData::getData)
- .map(jaxbJsonMarshaller::marshal)
- .map(JsonPath::parse)
- .map(DocumentContext::json)
- .orElse(null);
- }
- catch (Exception e)
- {
- LOGGER.debug("Unable to get json string from json data {}", jsonData);
- return null;
- }
- }
- }