Statistics
| Revision:

root / sqs-reader / src / main / java / net / sqs2 / omr / result / export / textarea / TextAreaHTMLWriter.java @ 1851

History | View | Annotate | Download (7.4 KB)

1
/**
2
 * TextAreaExporter.java
3
4
 Copyright 2009 KUBO Hiroya (hiroya@cuc.ac.jp).
5
6
 Licensed under the Apache License, Version 2.0 (the "License");
7
 you may not use this file except in compliance with the License.
8
 You may obtain a copy of the License at
9
10
 http://www.apache.org/licenses/LICENSE-2.0
11
12
 Unless required by applicable law or agreed to in writing, software
13
 distributed under the License is distributed on an "AS IS" BASIS,
14
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 See the License for the specific language governing permissions and
16
 limitations under the License.
17
18
 Author hiroya
19
 */
20
21
package net.sqs2.omr.result.export.textarea;
22
23
import java.io.File;
24
import java.io.IOException;
25
import java.io.PrintWriter;
26
import java.util.HashMap;
27
import java.util.LinkedList;
28
import java.util.List;
29
import java.util.Map;
30
31
import net.sqs2.omr.AppConstants;
32
import net.sqs2.omr.app.Messages;
33
import net.sqs2.omr.master.FormArea;
34
import net.sqs2.omr.master.FormMaster;
35
import net.sqs2.omr.model.SourceDirectory;
36
import net.sqs2.omr.result.export.HTMLWriter;
37
import net.sqs2.omr.result.export.ResultDirectoryUtil;
38
import net.sqs2.omr.result.export.html.HTMLReportExportModule;
39
import net.sqs2.util.StringUtil;
40
import freemarker.template.Template;
41
import freemarker.template.TemplateException;
42
43
public class TextAreaHTMLWriter extends HTMLWriter {
44
45
        public static final int NUM_IMAGES_PAR_PAGE = 50;
46
47
        Map<String, String> textAreaValueMap = new HashMap<String, String>();
48
49
        SourceDirectory sourceDirectory;
50
        File textAreaDirectoryFile;
51
        Template textAreaIndexTemplate;
52
        Template textAreaColumnTemplate;
53
        Template textAreaRowRangeTemplate;
54
55
        public TextAreaHTMLWriter(SourceDirectory sourceDirectory, File textAreaDirectoryFile, String skinName) throws IOException {
56
                super(skinName);
57
                this.sourceDirectory = sourceDirectory;
58
                this.textAreaDirectoryFile = textAreaDirectoryFile;
59
                this.textAreaIndexTemplate = this.loader.getTemplate("textAreaIndex.ftl", "UTF-8");
60
                this.textAreaColumnTemplate = this.loader.getTemplate("textAreaColumn.ftl", "UTF-8");
61
                this.textAreaRowRangeTemplate = this.loader.getTemplate("textAreaRowRange.ftl", "UTF-8");
62
        }
63
64
        private HashMap<String, Object> createFTLParameters(){
65
                HashMap<String, Object> map = new HashMap<String, Object>();
66
                super.registFTLParameters(map);
67
                for(String key: new String[]{
68
                                "rowNumberPrefixLabel",
69
                                "rowNumberSuffixLabel",
70
                }){
71
                        map.put(key, Messages.getString("result.textAreaIndex."+key));
72
                }
73
                map.put("skin", AppConstants.GROUP_ID);
74
                return map;
75
        }
76
77
        public void writeTextAreaIndexFile(FormMaster master, String spreadSheetSourceDirectoryPath) throws IOException, TemplateException {
78
                HashMap<String, Object> map = createFTLParameters();
79
                File textAreaIndexFile = new File(this.textAreaDirectoryFile, "index.html");
80
                List<FormArea> textareas = new LinkedList<FormArea>();
81
82
                for (String qid : master.getQIDSet()) {
83
                        FormArea defaultFormArea = master.getFormAreaList(qid).get(0);
84
                        if (defaultFormArea.isTextArea()) {
85
                                textareas.add(defaultFormArea);
86
                        }
87
                }
88
                
89
                registTextAreaIndexEntry(map, master, spreadSheetSourceDirectoryPath, textareas);
90
91
                map.put("sourceDirectory", this.sourceDirectory);
92
                
93
                PrintWriter textAreaIndexWriter = ResultDirectoryUtil.createPrintWriter(textAreaIndexFile);
94
                this.textAreaIndexTemplate.process(map, textAreaIndexWriter);
95
                textAreaIndexWriter.close();
96
        }
97
98
        void writeTextAreaColumnIndexFile(FormMaster master, TextAreaColumn textAreaColumn, String spreadSheetSourceDirectoryPath) throws IOException, TemplateException {
99
                HashMap<String, Object> map = createFTLParameters();
100
                HTMLReportExportModule.registTitle(master, map);
101
                registTextAreaColumnEntry(map, textAreaColumn, spreadSheetSourceDirectoryPath);
102
                writeTextAreaColumnFile(textAreaColumn, map);
103
        }
104
105
        void writeTextAreaRowRangeFile(FormMaster master, TextAreaColumn textAreaColumn, 
106
                        TextAreaRowGroup textAreaRowGroup, TextAreaRowRange textAreaRowRange, String path) throws IOException, TemplateException {
107
                HashMap<String,Object> map = createFTLParameters();
108
                writeTextAreaRowRangeFile(map, master, textAreaColumn, textAreaRowGroup, textAreaRowRange, path);
109
        }                        
110
        
111
        private HashMap<String, Object> registTextAreaColumnEntry(HashMap<String, Object> map, TextAreaColumn textAreaColumn, String spreadSheetSourceDirectoryPath) {
112
                map.put("textAreaColumnMetadata", textAreaColumn.getTextAreaColumnMetadata());
113
                map.put("textAreaColumn", textAreaColumn);
114
                map.put("path", spreadSheetSourceDirectoryPath);
115
                
116
                int numPathComponents = StringUtil.split(spreadSheetSourceDirectoryPath, File.separatorChar).size();
117
                
118
                StringBuilder returnPath = createRelativePathToParent(numPathComponents + 2);
119
                textAreaColumn.getTextAreaColumnMetadata().getSourceDirectoryPath();
120
121
                map.put("returnPath", returnPath.toString());
122
                map.put("resultFolder", AppConstants.RESULT_DIRNAME);
123
                return map;
124
        }
125
126
        private StringBuilder createRelativePathToParent(int numPathComponents) {
127
                StringBuilder returnPath = null;
128
                for(int i = 0; i < numPathComponents; i++){
129
                        if(returnPath == null){
130
                                returnPath = new StringBuilder();
131
                        }else{
132
                                returnPath.append("/");
133
                        }
134
                        returnPath.append("..");
135
                }
136
                return returnPath;
137
        }
138
139
        private HashMap<String, Object> registTextAreaIndexEntry(HashMap<String, Object> map, FormMaster master, String path, List<FormArea> textareas) {
140
                HTMLReportExportModule.registTitle(master, map);
141
                map.put("path", path);
142
                map.put("master", master);
143
                map.put("textareas", textareas);
144
                return map;
145
        }
146
147
        private void writeTextAreaColumnFile(TextAreaColumn textAreaColumn, HashMap<String, Object> map) throws IOException, TemplateException {
148
                File textAreaColumnFile = new File(new File(this.textAreaDirectoryFile, 
149
                                Integer.toString(textAreaColumn.getTextAreaColumnMetadata().getQuestionIndex())), "index.html");
150
                PrintWriter textAreaColumnWriter = ResultDirectoryUtil.createPrintWriter(textAreaColumnFile);
151
                this.textAreaColumnTemplate.process(map, textAreaColumnWriter);
152
                textAreaColumnWriter.close();
153
        }
154
155
        private void writeTextAreaRowRangeFile(HashMap<String, Object> map, FormMaster master, TextAreaColumn textAreaColumn, 
156
                        TextAreaRowGroup textAreaRowGroup, TextAreaRowRange textAreaRowRange, String path) throws IOException, TemplateException {
157
                registTextAreaRowRangeEntry(map, master, textAreaColumn, textAreaRowGroup, textAreaRowRange, path);
158
                File textAreaColumnDirectoryFile = new File(this.textAreaDirectoryFile, 
159
                                Integer.toString(textAreaColumn.getTextAreaColumnMetadata().getQuestionIndex()));
160
                textAreaColumnDirectoryFile.mkdirs();
161
                File textAreaRowRangeFile = new File(textAreaColumnDirectoryFile, 
162
                                textAreaRowRange.getTextAreaRowRangeMetadata().getRowRangeIndex()+ ".html");
163
                PrintWriter textAreaRowRangeWriter = ResultDirectoryUtil.createPrintWriter(textAreaRowRangeFile);
164
                this.textAreaRowRangeTemplate.process(map, textAreaRowRangeWriter);
165
                textAreaRowRangeWriter.close();
166
        }
167
168
        private void registTextAreaRowRangeEntry(HashMap<String, Object> map,
169
                        FormMaster master,
170
                        TextAreaColumn textAreaColumn,
171
                        TextAreaRowGroup textAreaRowGroup,
172
                        TextAreaRowRange textAreaRowRange,
173
                        String path) {
174
                HTMLReportExportModule.registTitle(master, map);
175
                map.put("numRowRangePages", textAreaColumn.getNumRowRangePages());
176
                map.put("textAreaColumnMetadata", textAreaColumn.getTextAreaColumnMetadata());
177
                map.put("textAreaRowRangeMetadata", textAreaRowRange.getTextAreaRowRangeMetadata());
178
                map.put("textAreaImageItemList", textAreaRowRange.getTextAreaImageItemList());
179
                map.put("path", path);
180
        }
181
}