2016. szeptember 21., szerda

Figure out if a field has been changed in JIRA event handler




At the previous post I have created an event handler for JIRA issues. Now, I will show how to check if a particular field has been changed during the event to be handled.

JIRA uses GenericValue objects from org.ofbiz.core.entity package to store event related information. GenericValue is basically a Map implementation to store all kind of data. So the key for finding the information we need is to know, which map keys we need to use to gain the data.

I have implemented a small utility class to find relevant elements in the issue event, and answer if a given field has been changed.


import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.ofbiz.core.entity.GenericEntityException;
import org.ofbiz.core.entity.GenericValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.issue.fields.CustomField;
import com.google.common.collect.ImmutableMap;

/**
 * Wrapper class to gain custom field specific information from the JIRA issue event
 * 
 * @author Peter Varga
 *
 */
class IssueEventWrapper {
 // keys to be searched in GenericValue objects
 private static final String KEY_CHANGE_ITEM = "ChangeItem";
 private static final String KEY_ID = "id";
 private static final String KEY_GROUP = "group";
 private static final String KEY_FIELD = "field";

 // Logger instance
 private static final Logger log = LoggerFactory.getLogger(IssueEventWrapper.class);

 private final IssueEvent issueEvent;

 /**
  * @param issueEvent
  */
 public IssueEventWrapper(IssueEvent issueEvent) {
  super();
  this.issueEvent = issueEvent;
 }

 /**
  * Answers if value of the given field has been changed during the issue event
  * 
  * @param customField
  * @return true if value of the given field has been changed
  */
 boolean fieldValueHasChanged(CustomField customField) {
  try {
   GenericValue changeLog = issueEvent.getChangeLog();
   if (changeLog == null) {
    return false;
   }

   List<GenericValue> changeItems = findChangeItems(changeLog);
   if (CollectionUtils.isEmpty(changeItems)) {
    return false;
   }

   for (GenericValue changedItem : changeItems) {
    // name of the field changed
    String field = changedItem.getString(KEY_FIELD);
    if (StringUtils.equals(field, customField.getFieldName())) {
     return true;
    }
   }
  } catch (GenericEntityException ex) {
   log.error(ex.getMessage(), ex);
  }
  return false;
 }

 /*
  * Returns list of change items, containing a single change information for each changed fields of the issue.
  */
 private List<GenericValue> findChangeItems(GenericValue changeLog) throws GenericEntityException {
  Object id = changeLog.get(KEY_ID);
  ImmutableMap<String, Object> map = new ImmutableMap.Builder<String, Object>().put(KEY_GROUP, id).build();
  return changeLog.internalDelegator.findByAnd(KEY_CHANGE_ITEM, map);
 }

}




Nincsenek megjegyzések:

Megjegyzés küldése