private static void setProperty(
     final RedmineAttribute redmineAttribute, final TaskAttribute root, final Issue issue)
     throws CoreException {
   final Field field = redmineAttribute.getAttributeField();
   if (!redmineAttribute.isReadOnly() && field != null) {
     final TaskAttribute taskAttribute = root.getAttribute(redmineAttribute.getTaskKey());
     if (taskAttribute != null) {
       try {
         switch (redmineAttribute) {
           case SUMMARY:
           case DESCRIPTION:
           case COMMENT:
             field.set(issue, taskAttribute.getValue());
             break;
           case PROGRESS:
             field.setInt(
                 issue,
                 taskAttribute.getValue().isEmpty()
                     ? 0
                     : Integer.parseInt(taskAttribute.getValue()));
             break;
           case ESTIMATED:
             if (!taskAttribute.getValue().isEmpty()) {
               field.set(issue, new Float(taskAttribute.getValue()));
             }
             break;
           default:
             if (redmineAttribute.getType().equals(TaskAttribute.TYPE_SINGLE_SELECT)
                 || redmineAttribute.getType().equals(TaskAttribute.TYPE_PERSON)
                 || redmineAttribute.getType().equals(IRedmineConstants.EDITOR_TYPE_PERSON)
                 || redmineAttribute == RedmineAttribute.PARENT) {
               final int idVal = RedmineUtil.parseIntegerId(taskAttribute.getValue());
               if (idVal > 0) {
                 field.setInt(issue, idVal);
               }
             } else if (redmineAttribute.getType().equals(TaskAttribute.TYPE_DATE)) {
               if (!taskAttribute.getValue().isEmpty()) {
                 field.set(issue, RedmineUtil.parseDate(taskAttribute.getValue()));
               }
             }
         }
       } catch (final Exception e) {
         final IStatus status =
             RedmineCorePlugin.toStatus(
                 e, Messages.ERRMSG_CANT_READ_PROPERTY_X, redmineAttribute.name());
         final ILogService log = RedmineCorePlugin.getLogService(IssueMapper.class);
         log.error(e, status.getMessage());
         throw new CoreException(status);
       }
     }
   }
 }
 private static void setValue(final TaskAttribute attribute, final String value) {
   if (value == null) {
     attribute.setValue(""); // $NON-NLS-1$
   } else if (attribute.getMetaData().getType() == null) {
     attribute.setValue(value);
   } else if (attribute.getMetaData().getType().equals(TaskAttribute.TYPE_MULTI_SELECT)) {
     attribute.addValue(value);
   } else if (attribute.getMetaData().getType().equals(TaskAttribute.TYPE_BOOLEAN)) {
     attribute.setValue(RedmineUtil.parseBoolean(value).toString());
   } else if (attribute.getMetaData().getType().equals(TaskAttribute.TYPE_DATE)
       || attribute.getMetaData().getType().equals(TaskAttribute.TYPE_DATETIME)) {
     setValue(attribute, RedmineUtil.parseRedmineDate(value));
   } else {
     attribute.setValue(value);
   }
 }
 private static String formatCustomValue(
     String value, final int customFieldId, final Configuration cfg) {
   final CustomField field = cfg.getCustomFields().getById(customFieldId);
   if (field != null) {
     switch (field.getFieldFormat()) {
       case DATE:
         value = value.isEmpty() ? "" : RedmineUtil.formatDate(RedmineUtil.parseDate(value));
         break; //$NON-NLS-1$
       case BOOL:
         value =
             Boolean.parseBoolean(value)
                 ? IRedmineConstants.BOOLEAN_TRUE_SUBMIT_VALUE
                 : IRedmineConstants.BOOLEAN_FALSE_SUBMIT_VALUE;
       default:
         return value;
     }
   }
   return value;
 }
  public static void restoreStructuredQueryPart(
      Query query,
      Configuration configuration,
      Map<IQueryField, ComboViewer> operators,
      Map<IQueryField, StructuredViewer> values) {
    for (Entry<IQueryField, StructuredViewer> entry : values.entrySet()) {
      IQueryField queryField = entry.getKey();
      QueryFilter queryFilter = query.getQueryFilter(queryField);
      if (queryFilter == null) {
        continue;
      }

      StructuredViewer viewer = entry.getValue();
      ComboViewer operatorCombo = operators.get(queryField);

      operatorCombo.setSelection(new StructuredSelection(queryFilter.getOperator()));
      if (queryFilter.getOperator().isValueBased()) {
        viewer.getControl().setEnabled(true);

        List<String> filterValues = queryFilter.getValues();
        if (queryField instanceof QueryField) {
          List<Object> properties = new ArrayList<Object>(filterValues.size());
          for (String value : filterValues) {
            if (RedmineUtil.isInteger(value)) {
              Property property =
                  queryFieldValue2Property(
                      RedmineUtil.parseIntegerId(value), (QueryField) queryField, configuration);
              if (property != null) {
                properties.add(property);
              }
            } else {
              properties.add(value);
            }
            viewer.setSelection(new StructuredSelection(properties));
          }
        } else {
          viewer.setSelection(new StructuredSelection(filterValues));
        }
      }
    }
  }
  public static Issue createIssue(
      final TaskRepository repository,
      final TaskData taskData,
      final Set<TaskAttribute> oldAttributes,
      final Configuration cfg)
      throws CoreException {
    final Issue issue =
        taskData.getTaskId().isEmpty()
            ? new Issue()
            : new Issue(RedmineUtil.parseIntegerId(taskData.getTaskId()));

    final TaskAttribute root = taskData.getRoot();
    TaskAttribute taskAttribute = null;

    /* Default Attributes */
    for (final RedmineAttribute redmineAttribute : RedmineAttribute.values()) {
      if (!redmineAttribute.isOperationValue() && !redmineAttribute.isReadOnly()) {
        setProperty(redmineAttribute, root, issue);
      }
    }

    /* Watcher */
    final TaskAttribute watchersAttribute =
        root.getAttribute(RedmineAttribute.WATCHERS.getTaskKey());
    if (watchersAttribute != null) {
      final LinkedHashSet<String> watchers =
          new LinkedHashSet<String>(watchersAttribute.getValues());

      final TaskAttribute newWatcherAttribute =
          watchersAttribute.getAttribute(RedmineAttribute.WATCHERS_ADD.getTaskKey());
      if (newWatcherAttribute != null && !newWatcherAttribute.getMetaData().isReadOnly()) {
        issue.setWatchersAddAllowed(true);
        for (final String newWatcher : newWatcherAttribute.getValues()) {
          watchers.add(newWatcher);
        }
      }

      final TaskAttribute oldWatcherAttribute =
          watchersAttribute.getAttribute(RedmineAttribute.WATCHERS_REMOVE.getTaskKey());
      if (oldWatcherAttribute != null && !oldWatcherAttribute.getMetaData().isReadOnly()) {
        issue.setWatchersDeleteAllowed(true);
        for (final String oldWatcher : oldWatcherAttribute.getValues()) {
          watchers.remove(oldWatcher);
        }
      }

      if (watchers.size() > 0) {
        final int[] watcherIds = new int[watchers.size()];
        int lv = 0;
        for (final String idVal : watchers) {
          watcherIds[lv++] = Integer.parseInt(idVal);
        }
        issue.setWatcherIds(watcherIds);
      }
    }

    /* Custom Attributes */
    final int[] customFieldIds =
        cfg.getProjects()
            .getById(issue.getProjectId())
            .getCustomFieldIdsByTrackerId(issue.getTrackerId());
    if (customFieldIds != null && customFieldIds.length > 0) {
      final CustomValues customValues = new CustomValues();
      issue.setCustomValues(customValues);
      for (final int customFieldId : customFieldIds) {
        taskAttribute =
            root.getAttribute(IRedmineConstants.TASK_KEY_PREFIX_ISSUE_CF + customFieldId);
        if (taskAttribute != null) {
          if (TaskAttribute.TYPE_MULTI_SELECT.equals(taskAttribute.getMetaData().getType())) {
            final List<String> values = taskAttribute.getValues();
            for (final String value : values) {
              customValues.setCustomValue(
                  customFieldId, formatCustomValue(value, customFieldId, cfg));
            }
          } else {
            customValues.setCustomValue(
                customFieldId, formatCustomValue(taskAttribute.getValue(), customFieldId, cfg));
          }
        }
      }
    }

    /* Operations */
    taskAttribute = root.getMappedAttribute(TaskAttribute.OPERATION);
    if (taskAttribute != null) {
      final RedmineOperation redmineOperation =
          RedmineOperation.fromTaskKey(taskAttribute.getValue());
      taskAttribute = root.getAttribute(TaskAttribute.PREFIX_OPERATION + taskAttribute.getValue());

      if (redmineOperation != null && taskAttribute != null) {
        String value = null;

        if (redmineOperation.isAssociated()) {
          taskAttribute = root.getAttribute(redmineOperation.getInputId());
          if (taskAttribute != null) {
            value = taskAttribute.getValue();
          }
        } else if (redmineOperation.needsRestoreValue()) {
          value =
              taskAttribute
                  .getMetaData()
                  .getValue(IRedmineConstants.TASK_ATTRIBUTE_OPERATION_RESTORE);
        }

        if (value != null) {
          final RedmineAttribute redmineAttribute =
              RedmineAttribute.fromTaskKey(redmineOperation.getInputId());
          setProperty(redmineAttribute, root, issue);
        }
      }
    }

    setTaskKind(issue, cfg, root);

    return issue;
  }