1 package com.atlassian.jira.export;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import org.apache.log4j.Category;
13
14 import com.atlassian.jira.ManagerFactory;
15 import com.atlassian.jira.config.properties.ApplicationProperties;
16 import com.atlassian.jira.issue.Issue;
17 import com.atlassian.jira.issue.IssueManager;
18 import com.atlassian.jira.issue.MutableIssue;
19
20 public abstract class AIssueExportManager {
21
22 private static final Category log = Category.getInstance(AIssueExportManager.class);
23
24 private static final String DEFAULT_DATE_FORMAT = "yyyy-MMM-dd-HHmm";
25
26 public static final String FS = ";";
27
28 public static final String FD = "\"";
29
30 protected static final String NL = System.getProperty("line.separator");
31
32 private PrintWriter writer = null;
33
34 protected int exportedItem = 0;
35
36 private IPathExportProvider pathExportProvider;
37
38 private IRequestProvider requestProvider;
39
40 protected final ApplicationProperties applicationProperties = ManagerFactory.getApplicationProperties();
41
42 protected IssueManager issueManager = ManagerFactory.getIssueManager();
43
44
45
46
47
48 public void doExport(final List _issues) throws ExportException {
49 initExport();
50
51 for (Iterator iterator = _issues.iterator(); iterator.hasNext();) {
52 Issue issue = (Issue) iterator.next();
53 MutableIssue mIssue = issueManager.getIssueObject(issue.getId());
54 doExport(mIssue);
55 exportedItem++;
56 }
57 endExport();
58 }
59
60
61
62
63 protected void initExport() throws ExportException {
64
65 }
66
67
68
69
70 protected void endExport() throws ExportException {
71 if (writer!=null) {
72 writer.close();
73 }
74 writer = null;
75 }
76
77
78
79
80
81 public void doExport(final MutableIssue _issue) throws ExportException {
82 if (isExportable(_issue)) {
83 exportData(_issue);
84 }
85 }
86
87
88
89
90
91 protected boolean isExportable(Issue _issue) throws ExportException {
92 return true;
93 }
94
95 protected void exportData(final Issue _issue) throws ExportException {
96 writeHeader(_issue);
97 writeData(_issue);
98 writeFooter(_issue);
99 }
100
101
102
103 protected abstract void writeHeader(final Issue _issue) throws ExportException;
104
105
106
107 protected abstract void writeData(final Issue _issue) throws ExportException;
108
109
110
111 protected abstract void writeFooter(final Issue _issue) throws ExportException;
112
113
114
115
116 protected PrintWriter getWriter() throws ExportException {
117 try {
118 if (writer == null) {
119 writer = openWriter();
120 }
121 } catch (Exception e) {
122 throw new ExportException(e);
123 }
124 return writer;
125 }
126
127
128
129
130
131 protected PrintWriter openWriter() throws ExportException {
132 OutputStream outputStream = null;
133 PrintWriter writer = null;
134 if (writer == null) {
135 try {
136 outputStream = new FileOutputStream(new File(getExportFullFileName()));
137
138 writer = new PrintWriter(new OutputStreamWriter(outputStream, applicationProperties.getEncoding()));
139
140 } catch (IOException e) {
141 try {
142 if (outputStream != null) {
143 outputStream.close();
144 }
145 } catch (IOException ioe) {
146 log.error(ioe);
147 ioe.printStackTrace();
148 throw new ExportException(ioe);
149 }
150 }
151 }
152 return writer;
153 }
154
155
156
157
158
159 public String getExportFullFileName() throws ExportException {
160 return getPathExportProvider().getExportFullFileName();
161 }
162
163
164
165
166
167 public IPathExportProvider getPathExportProvider() throws ExportException {
168 if (pathExportProvider == null) {
169 throw new ExportException("PathExportProvider is unknown ...");
170 }
171 return pathExportProvider;
172 }
173
174
175
176
177 public void setPathExportProvider(IPathExportProvider pathExportProvider) {
178 this.pathExportProvider = pathExportProvider;
179 }
180
181
182
183
184 public IRequestProvider getRequestProvider() {
185 return requestProvider;
186 }
187
188
189
190
191 public void setRequestProvider(IRequestProvider requestProvider) {
192 this.requestProvider = requestProvider;
193 }
194
195 }