OSDN Git Service

[Moxkiriya7]
[moxkiriya7/Moxkiriya.git] / Moxkiriya7 / src / com / wiki / standalone / moxkiriya / WikiHistory.java
1 /**
2  * Moxkiriya standalone Wiki.
3  * WikiHistory.
4  * 
5  * @author Ryuhei Terada
6  * See the '<a href="{@docRoot}/copyright.html">Copyright</a>'
7  */
8 package com.wiki.standalone.moxkiriya;
9
10 import java.util.ArrayList;
11
12 import javax.jcr.Property;
13
14 /**
15  * Manage wiki history.
16  * 
17  *
18  */
19 public class WikiHistory {
20         /** History list*/
21         private ArrayList<PageData> histories_ = new ArrayList<PageData>();
22
23         /** Position on history list. */
24         private int position_ = 0;
25         
26         /**
27          * Constructor.
28          */
29         public WikiHistory() {
30                 histories_.clear();
31         }
32
33         /**
34          * Is there an older node on a history list.
35          * @return boolean
36          */
37         public boolean canBack() {
38                 boolean canBack = false;
39                 
40                 if(histories_.size() > 1) {
41                         if(position_ > 0) {
42                                 canBack = true;
43                         }
44                 }
45                 
46                 return canBack;
47         }
48
49         /**
50          * Is there an newer node on a history list.
51          * @return boolean
52          */
53         public boolean canForward() {
54                 boolean canForward = false;
55                 
56                 if(histories_.size() > 0) {
57                         if(position_ < histories_.size() - 1) {
58                                 canForward = true;
59                         }
60                 }
61                 
62                 return canForward;
63         }
64
65         /**
66          * Execute history back.
67          * @return PageData
68          */
69         public PageData back() {
70                 PageData pageData = null;
71
72                 if(canBack() == true) {
73                         position_--;
74                         pageData = histories_.get(position_);
75                 }
76                 
77                 return pageData;
78         }
79
80         /**
81          * Execute history forward.
82          * @return PageData
83          */
84         public PageData forward() {
85                 PageData pageData = null;
86                 
87                 if(canForward() == true) {
88                         position_++;
89                         pageData = histories_.get(position_);
90                 }
91                 
92                 return pageData;
93         }
94
95         /**
96          * Add PageData to history list.
97          * @param pageData
98          * @throws Exception
99          */
100         public void add(PageData pageData) throws Exception {
101                 if(histories_.size() > 0) {
102                         PageData latestPage = histories_.get(position_);
103                         String   uuid       = latestPage.getNode().getProperty(Property.JCR_UUID).getString();
104
105                         if(pageData.getNode().getProperty(Property.JCR_UUID).getString().equals(uuid) == true) {
106                                 /*
107                                  * History中に同じページを連続登録しようとした場合、
108                                  */
109                                 histories_.remove(position_);
110                         }
111                         else {
112                                 position_++;                            
113                         }
114                 }
115                 
116                 for(int count = position_; count < histories_.size(); count++) {
117                         histories_.remove(count);
118                 }
119                 
120                 histories_.add(pageData);
121                 position_ = histories_.size() - 1;
122         }
123
124         /**
125          * Clear history.
126          */
127         public void clear() {
128                 histories_.clear();
129                 position_ = 0;
130         }
131 }