package tw.kewang.redmine.appender;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import com.taskadapter.redmineapi.IssueManager;
import com.taskadapter.redmineapi.RedmineException;
import com.taskadapter.redmineapi.RedmineManager;
import com.taskadapter.redmineapi.RedmineManagerFactory;
import com.taskadapter.redmineapi.bean.Issue;
import com.taskadapter.redmineapi.bean.IssueFactory;

public class RedmineAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
    private String url;
    private String apiKey;
    private int projectId;
    private RedmineManager redmineManager;
    private IssueManager issueManager;

    @Override
    public void start() {
        String propertyRedmineUrl = getContext().getProperty("REDMINE_URL");
        String propertyRedmineApiKey = getContext().getProperty("REDMINE_API_KEY");
        int propertyRedmineProjectId = -1;

        try {
            propertyRedmineProjectId = Integer.valueOf(getContext().getProperty("REDMINE_PROJECT_ID"));
        } catch (Exception e) {
            addError("Exception", e);
        }

        if (checkProperty(propertyRedmineUrl, propertyRedmineApiKey, propertyRedmineProjectId)) {
            addError("No set REDMINE_URL / REDMINE_API_KEY / REDMINE_PROJECT_ID [" + name + "].");

            return;
        }

        url = propertyRedmineUrl;
        apiKey = propertyRedmineApiKey;
        projectId = propertyRedmineProjectId;
        redmineManager = RedmineManagerFactory.createWithApiKey(url, apiKey);
        issueManager = redmineManager.getIssueManager();

        super.start();
    }

    private boolean checkProperty(String propertyRedmineUrl, String propertyRedmineApiKey, int propertyRedmineProjectId) {
        return propertyRedmineUrl == null || propertyRedmineUrl.length() == 0 || propertyRedmineApiKey == null
                || propertyRedmineApiKey.length() == 0 || propertyRedmineProjectId == -1;
    }

    @Override
    public void append(ILoggingEvent event) {
        createIssue(event);
    }

    private void createIssue(ILoggingEvent event) {
        Issue issue = IssueFactory.create(projectId, event.getLoggerName());

        issue.setDescription(event.getFormattedMessage());

        try {
            issueManager.createIssue(issue);
        } catch (RedmineException e) {
            addError("Exception", e);
        }
    }
}
@@ -594,24 +594,29 @@
 nt projectId
+ = -1
 ;%0A    privat
@@ -740,752 +740,136 @@
     
-String propertyRedmineUrl = getContext().getProperty(%22REDMINE_URL%22);%0A        String propertyRedmineApiKey = getContext().getProperty(%22REDMINE_API_KEY%22);%0A        int propertyRedmineProjectId = -1;%0A%0A        try %7B%0A            propertyRedmineProjectId = Integer.valueOf(getContext().getProperty(%22REDMINE_PROJECT_ID%22));%0A        %7D catch (Exception e) %7B%0A            addError(%22Exception%22, e);%0A        %7D%0A%0A        if (checkProperty(propertyRedmineUrl, propertyRedmineApiKey, propertyRedmineProjectId)) %7B%0A            addError(%22No set REDMINE_URL / REDMINE_API_KEY / REDMINE_PROJECT_ID %5B%22 + name + %22%5D.%22);%0A%0A            return;%0A        %7D%0A%0A        url = propertyRedmineUrl;%0A        apiKey = propertyRedmineApiKey;%0A        projectId = propertyRedmineProjectId;
+if (!checkProperty()) %7B%0A            addError(%22No set url / apiKey / projectId %5B%22 + name + %22%5D.%22);%0A%0A            return;%0A        %7D%0A
 %0A   
@@ -1069,93 +1069,8 @@
 rty(
-String propertyRedmineUrl, String propertyRedmineApiKey, int propertyRedmineProjectId
 ) %7B%0A
@@ -1088,54 +1088,24 @@
 urn 
-propertyRedmineUrl =
+url !
 = null 
-%7C%7C propertyRedmineU
+&& u
 rl.l
@@ -1116,81 +1116,35 @@
 h() 
-=
+!
 = 0 
-%7C%7C propertyRedmineA
+&& a
 piKey 
-=
+!
 = null
-%0A                %7C%7C propertyRedmineA
+ && a
 piKe
@@ -1158,42 +1158,27 @@
 h() 
-=
+!
 = 0 
-%7C%7C
+&&
  pro
-pertyRedminePro
 jectId 
-=
+!
 = -1
@@ -1622,10 +1622,430 @@
  %7D%0A    %7D
+%0A%0A    public void setUrl(String url) %7B%0A        this.url = url;%0A    %7D%0A%0A    public String getUrl() %7B%0A        return url;%0A    %7D%0A%0A    public void setApiKey(String apiKey) %7B%0A        this.apiKey = apiKey;%0A    %7D%0A%0A    public String getApiKey() %7B%0A        return apiKey;%0A    %7D%0A%0A    public void setProjectId(int projectId) %7B%0A        this.projectId = projectId;%0A    %7D%0A%0A    public int getProjectId() %7B%0A        return projectId;%0A    %7D
 %0A%7D

Use inject JavaBeans to access properties