OSDN Git Service

改行コード指定
[jindolf/JinArchiver.git] / src / main / java / jp / sourceforge / jindolf / archiver / PeriodData.java
1 /*
2  * period model
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf.archiver;
9
10 import java.io.IOException;
11 import java.io.Writer;
12 import java.net.URI;
13 import java.util.LinkedList;
14 import java.util.List;
15 import jp.sourceforge.jindolf.corelib.DisclosureType;
16 import jp.sourceforge.jindolf.corelib.SysEventType;
17 import jp.sourceforge.jindolf.parser.DecodedContent;
18
19 /**
20  * Periodモデル。
21  */
22 public class PeriodData{
23
24     private final VillageData parent;
25     private final PeriodResource resource;
26     private DecodedContent loginName = new DecodedContent("");
27     private int commitMonth;
28     private int commitDay;
29     private int commitHour;
30     private int commitMinute;
31     private DisclosureType disclosureType = DisclosureType.HOT;
32     private boolean hasMurderResult = false;
33
34     private final List<TopicData> topicList = new LinkedList<TopicData>();
35
36     /**
37      * コンストラクタ。
38      * @param parent 所属村
39      * @param resource ロード元情報
40      */
41     public PeriodData(VillageData parent, PeriodResource resource){
42         super();
43         this.parent = parent;
44         this.resource = resource;
45         return;
46     }
47
48     /**
49      * ロード時のログイン名を取得する。
50      * @return ログイン名
51      */
52     public DecodedContent getLoginName(){
53         return this.loginName;
54     }
55
56     /**
57      * ロード時のログイン名を設定する。
58      * @param loginName ログイン名
59      */
60     public void setLoginName(DecodedContent loginName){
61         this.loginName = loginName;
62         return;
63     }
64
65     /**
66      * コミット月を取得する。
67      * @return コミット月
68      */
69     public int getCommitMonth(){
70         return this.commitMonth;
71     }
72
73     /**
74      * コミット月を設定する。
75      * @param commitMonth コミット月
76      */
77     public void setCommitMonth(int commitMonth){
78         this.commitMonth = commitMonth;
79         return;
80     }
81
82     /**
83      * コミット日を取得する。
84      * @return コミット日
85      */
86     public int getCommitDay(){
87         return this.commitDay;
88     }
89
90     /**
91      * コミット日を設定する。
92      * @param commitDay コミット日
93      */
94     public void setCommitDay(int commitDay){
95         this.commitDay = commitDay;
96         return;
97     }
98
99     /**
100      * コミット時を取得する。
101      * @return コミット時
102      */
103     public int getCommitHour(){
104         return this.commitHour;
105     }
106
107     /**
108      * コミット時を設定する。
109      * @param commitHour コミット時
110      */
111     public void setCommitHour(int commitHour){
112         this.commitHour = commitHour;
113         return;
114     }
115
116     /**
117      * コミット分を取得する。
118      * @return コミット分
119      */
120     public int getCommitMinute(){
121         return this.commitMinute;
122     }
123
124     /**
125      * コミット分を設定する。
126      * @param commitMinute コミット分
127      */
128     public void setCommitMinute(int commitMinute){
129         this.commitMinute = commitMinute;
130         return;
131     }
132
133     /**
134      * 開示状況を取得する。
135      * @return 開示状況
136      */
137     public DisclosureType getDisclosureType(){
138         return this.disclosureType;
139     }
140
141     /**
142      * 開示状況を設定する。
143      * @param type 開示状況
144      */
145     public void setDisclosureType(DisclosureType type){
146         this.disclosureType = type;
147         return;
148     }
149
150     /**
151      * 襲撃結果イベントが既に格納されているか確認する。
152      * @return 襲撃結果があればtrue
153      */
154     public boolean hasMurderResult(){
155         return this.hasMurderResult;
156     }
157
158     /**
159      * TopicDataを追加する。
160      * 襲撃結果の有無も判定される。
161      * @param topicData TopiData
162      */
163     public void addTopicData(TopicData topicData){
164         this.topicList.add(topicData);
165
166         if(topicData instanceof EventData){
167             EventData event = (EventData) topicData;
168             SysEventType type = event.getEventType();
169             if(   type == SysEventType.MURDERED
170                || type == SysEventType.NOMURDER){
171                 this.hasMurderResult = true;
172             }
173         }
174
175         return;
176     }
177
178     /**
179      * period要素をXML出力する。
180      * @param writer 出力先
181      * @throws IOException 出力エラー
182      */
183     public void dumpXml(Writer writer) throws IOException{
184         writer.append("<period\n");
185
186         String ptype;
187         switch(this.resource.getPeriodType()){
188         case PROLOGUE:
189             ptype = "prologue";
190             break;
191         case PROGRESS:
192             ptype = "progress";
193             break;
194         case EPILOGUE:
195             ptype = "epilogue";
196             break;
197         default:
198             throw new IllegalArgumentException();
199         }
200
201         XmlUtils.indent(writer, 1);
202         XmlUtils.attrOut(writer, "type", ptype);
203
204         writer.append(' ');
205         XmlUtils.attrOut(writer,
206                 "day", Integer.toString(this.resource.getDay()));
207         writer.append('\n');
208
209         if(this.disclosureType != DisclosureType.COMPLETE){
210             XmlUtils.indent(writer, 1);
211             XmlUtils.attrOut(writer,
212                     "disclosure", this.disclosureType.getXmlName());
213             writer.append('\n');
214         }
215
216         XmlUtils.indent(writer, 1);
217         XmlUtils.dateAttrOut(writer, "nextCommitDay",
218                              this.commitMonth, this.commitDay);
219
220         writer.append(' ');
221         XmlUtils.timeAttrOut(writer,
222                              "commitTime",
223                              this.commitHour, this.commitMinute);
224         writer.append('\n');
225
226         URI baseUri   = URI.create(this.parent.getBaseUri());
227         URI periodUri = URI.create(this.resource.getOrigUrlText());
228         URI relativeUri = baseUri.relativize(periodUri);
229         XmlUtils.indent(writer, 1);
230         XmlUtils.attrOut(writer, "sourceURI", relativeUri.toString());
231         writer.append('\n');
232
233         long downTimeMs = this.resource.getDownTimeMs();
234         XmlUtils.indent(writer, 1);
235         XmlUtils.dateTimeAttr(writer, "loadedTime", downTimeMs);
236         writer.append('\n');
237
238         if(this.loginName.length() > 0){
239             XmlUtils.indent(writer, 1);
240             XmlUtils.attrOut(writer, "loadedBy", this.loginName.toString());
241             writer.append('\n');
242         }
243
244         writer.append(">\n\n");
245
246         for(TopicData topic : this.topicList){
247             topic.dumpXml(writer);
248             writer.append('\n');
249             writer.flush();
250         }
251
252         writer.append("</period>\n");
253
254         return;
255     }
256
257 }