001: package ru.emdev.EmForge.wiki.providers.mapprovider;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.Collection;
006: import java.util.Date;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.Properties;
012: import java.util.Set;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016:
017: import com.ecyrd.jspwiki.NoRequiredPropertyException;
018: import com.ecyrd.jspwiki.QueryItem;
019: import com.ecyrd.jspwiki.WikiEngine;
020: import com.ecyrd.jspwiki.WikiPage;
021: import com.ecyrd.jspwiki.providers.ProviderException;
022: import com.ecyrd.jspwiki.providers.WikiPageProvider;
023:
024: /** Map Page Provider used for storing wiki-pages in memory
025: *
026: * May be used for test purposes
027: */
028: public class MapPageProvider implements WikiPageProvider {
029: protected final Log logger = LogFactory.getLog(getClass());
030:
031: public class WikiStorage {
032: WikiPage page;
033: String content;
034:
035: public WikiStorage(WikiPage page, String content) {
036: this .page = page;
037: this .content = content;
038: }
039:
040: public WikiPage getPage() {
041: return page;
042: }
043:
044: public String getContent() {
045: return content;
046: }
047:
048: public void setPage(WikiPage page) {
049: this .page = page;
050: }
051: }
052:
053: // Map for storing wiki pages with contents
054: private Map<String, Collection> m_pagesMap = null;
055: private Map<String, Integer> m_latestVersion = null;
056:
057: public MapPageProvider() {
058: m_pagesMap = new HashMap<String, Collection>();
059: m_latestVersion = new HashMap<String, Integer>();
060: }
061:
062: @SuppressWarnings("unchecked")
063: public void putPageText(WikiPage i_page, String i_text)
064: throws ProviderException {
065: String pageName = i_page.getName();
066: WikiPage page = null;
067: Collection<WikiStorage> versList = new ArrayList<WikiStorage>();
068: if (m_pagesMap.containsKey(pageName)) {
069: page = (WikiPage) i_page.clone();
070: int latestVersion = m_latestVersion.get(pageName);
071: page.setVersion(latestVersion + 1);
072: m_latestVersion.put(pageName, latestVersion + 1);
073: versList = m_pagesMap.get(pageName);
074: } else {
075: page = i_page;
076: page.setVersion(1);
077: m_latestVersion.put(pageName, 1);
078: }
079: page.setLastModified(new Date());
080: if (i_text != null) {
081: page.setSize(i_text.getBytes().length);
082: } else {
083: page.setSize(0);
084: }
085: versList.add(new WikiStorage(page, i_text));
086: m_pagesMap.put(pageName, versList);
087: }
088:
089: public boolean pageExists(String i_name) {
090: return m_pagesMap.containsKey(i_name);
091: }
092:
093: public Collection findPages(QueryItem[] arg0) {
094: // TODO Auto-generated method stub
095: return null;
096: }
097:
098: public WikiPage getPageInfo(String i_name, int i_version)
099: throws ProviderException {
100: logger.debug("Get information about page " + i_name
101: + " version " + i_version);
102: WikiPage result = null;
103: WikiStorage wikiStorage = getWiki(i_name, m_pagesMap
104: .get(i_name), i_version);
105: if (wikiStorage != null) {
106: result = wikiStorage.getPage();
107: }
108: return result;
109: }
110:
111: /* WikiPageProvider method implementation */
112: public Collection getAllPages() throws ProviderException {
113: Collection<WikiPage> result = new ArrayList<WikiPage>();
114: Set<String> keyset = m_pagesMap.keySet();
115: if (!keyset.isEmpty()) {
116: Iterator iterator = keyset.iterator();
117: while (iterator.hasNext()) {
118: String pageName = (String) iterator.next();
119: result.add(getWiki(pageName, m_pagesMap.get(pageName),
120: WikiPageProvider.LATEST_VERSION).getPage());
121: }
122: }
123:
124: return result;
125: }
126:
127: /** returns page with needed version; i_version = -1 - return the last version
128: * @author spopov
129: */
130: private WikiStorage getWiki(String i_name, Collection i_wiki,
131: long i_version) {
132:
133: if (i_wiki == null) {
134: return null;
135: }
136:
137: WikiStorage result = null;
138: long version;
139: if (i_version == WikiPageProvider.LATEST_VERSION) {
140: version = m_latestVersion.get(i_name);
141: } else {
142: version = i_version;
143: }
144:
145: Iterator iterator = i_wiki.iterator();
146: while (iterator.hasNext()) {
147: WikiStorage wikiStorage = (WikiStorage) iterator.next();
148: if (wikiStorage.getPage().getVersion() == version) {
149: result = wikiStorage;
150: break;
151: }
152: }
153:
154: return result;
155: }
156:
157: // [AKO] PageDAO method implementation
158: /* public Collection getPages() {
159: return getHibernateTemplate().loadAll(WikiPageImpl.class);
160: } */
161:
162: public Collection getAllChangedSince(Date i_date) {
163: // TODO Auto-generated method stub
164: return null;
165: }
166:
167: public int getPageCount() throws ProviderException {
168: return m_pagesMap.size();
169: }
170:
171: @SuppressWarnings("unchecked")
172: public List getVersionHistory(String i_name)
173: throws ProviderException {
174: ArrayList<Object> result = new ArrayList<Object>();
175: Collection<WikiStorage> wikis = m_pagesMap.get(i_name);
176: for (WikiStorage wikiStorage : wikis) {
177: result.add(wikiStorage.getPage());
178: }
179: return result;
180: }
181:
182: public String getPageText(String i_name, int i_version)
183: throws ProviderException {
184: WikiStorage wikiStorage = getWiki(i_name, m_pagesMap
185: .get(i_name), i_version);
186: if (wikiStorage == null) {
187: return null;
188: }
189: return wikiStorage.getContent();
190: }
191:
192: public void deleteVersion(String i_name, int i_version)
193: throws ProviderException {
194: /// versionning is not supported
195: }
196:
197: public void deletePage(String i_name) throws ProviderException {
198: m_pagesMap.remove(i_name);
199: }
200:
201: /* [AKO]: PageDAO method implementation
202: /* public void deletePage(Page page) {
203:
204: } */
205: WikiEngine m_wikiEngine;
206:
207: public void initialize(WikiEngine engine, Properties arg1)
208: throws NoRequiredPropertyException, IOException {
209: m_wikiEngine = engine;
210: }
211:
212: public String getProviderInfo() {
213: return "Map Page Provider";
214: }
215:
216: @SuppressWarnings("unchecked")
217: public void movePage(String from, String to)
218: throws ProviderException {
219: logger.debug("move page");
220: Collection<WikiStorage> oldVers = m_pagesMap.get(from);
221: Collection<WikiStorage> newVers = new ArrayList<WikiStorage>();
222: for (WikiStorage wikiStorage : oldVers) {
223: // copy page data to new page
224: WikiPage page = new WikiPage(m_wikiEngine, to);
225: page.setAcl(wikiStorage.getPage().getAcl());
226: page.setAuthor(wikiStorage.getPage().getAuthor());
227: page.setLastModified(wikiStorage.getPage()
228: .getLastModified());
229: page.setSize(wikiStorage.getPage().getSize());
230: page.setVersion(wikiStorage.getPage().getVersion());
231: newVers
232: .add(new WikiStorage(page, wikiStorage.getContent()));
233: }
234: // delete renamed page
235: m_pagesMap.remove(from);
236: // put new page with new name
237: m_pagesMap.put(to, newVers);
238: // refresh key in Latest Version Map
239: int lastVers = m_latestVersion.get(from);
240: m_latestVersion.remove(from);
241: m_latestVersion.put(to, lastVers);
242: }
243:
244: }
|