001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2007
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.pages.model;
025:
026: import java.util.Collection;
027: import java.util.Date;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Locale;
031: import java.util.Map;
032:
033: import org.riotfamily.common.util.FormatUtils;
034: import org.riotfamily.components.model.ComponentVersion;
035: import org.riotfamily.components.model.VersionContainer;
036:
037: /**
038: * @author Felix Gnass [fgnass at neteye dot de]
039: * @author Jan-Frederic Linde [jfl at neteye dot de]
040: * @since 6.5
041: */
042: public class Page {
043:
044: public static final String TITLE_PROPERTY = "title";
045:
046: private Long id;
047:
048: private PageNode node;
049:
050: private Site site;
051:
052: private String pathComponent;
053:
054: private boolean hidden;
055:
056: private boolean folder;
057:
058: private String path;
059:
060: private boolean wildcardInPath;
061:
062: private boolean published;
063:
064: private Date creationDate;
065:
066: private VersionContainer versionContainer;
067:
068: public Page() {
069: }
070:
071: public Page(String pathComponent, Site site) {
072: this .pathComponent = pathComponent;
073: this .site = site;
074: }
075:
076: public Long getId() {
077: return this .id;
078: }
079:
080: public void setId(Long id) {
081: this .id = id;
082: }
083:
084: public PageNode getNode() {
085: return this .node;
086: }
087:
088: public void setNode(PageNode node) {
089: this .node = node;
090: }
091:
092: public Site getSite() {
093: return this .site;
094: }
095:
096: public void setSite(Site site) {
097: this .site = site;
098: }
099:
100: public Locale getLocale() {
101: return site.getLocale();
102: }
103:
104: public Date getCreationDate() {
105: return creationDate;
106: }
107:
108: public void setCreationDate(Date creationDate) {
109: this .creationDate = creationDate;
110: }
111:
112: public String getPathComponent() {
113: return pathComponent;
114: }
115:
116: public void setPathComponent(String pathComponent) {
117: this .pathComponent = pathComponent;
118: }
119:
120: public boolean isHidden() {
121: return this .hidden;
122: }
123:
124: public void setHidden(boolean hidden) {
125: this .hidden = hidden;
126: }
127:
128: /**
129: * Returns whether the page only acts as container for other pages and
130: * has no own content.
131: */
132: public boolean isFolder() {
133: return this .folder;
134: }
135:
136: public void setFolder(boolean folder) {
137: this .folder = folder;
138: }
139:
140: public String getPath() {
141: if (path == null) {
142: path = buildPath();
143: }
144: return path;
145: }
146:
147: public void setPath(String path) {
148: this .path = path;
149: }
150:
151: public String getFullPath() {
152: if (site.getPathPrefix() != null) {
153: return site.getPathPrefix() + getPath();
154: }
155: return getPath();
156: }
157:
158: public boolean isWildcard() {
159: return pathComponent.indexOf("@{") != -1;
160: }
161:
162: public boolean isWildcardInPath() {
163: return this .wildcardInPath;
164: }
165:
166: public void setWildcardInPath(boolean wildcardInPath) {
167: this .wildcardInPath = wildcardInPath;
168: }
169:
170: public String buildPath() {
171: StringBuffer path = new StringBuffer();
172: wildcardInPath = false;
173: Page page = this ;
174: while (page != null) {
175: path.insert(0, page.getPathComponent());
176: path.insert(0, '/');
177: wildcardInPath |= page.isWildcard();
178: page = page.getParentPage();
179: }
180: return path.toString();
181: }
182:
183: public Page getParentPage() {
184: PageNode parentNode = node.getParent();
185: if (parentNode == null) {
186: return null;
187: }
188: return parentNode.getPage(site);
189: }
190:
191: public List getChildPages() {
192: return node.getChildPages(site);
193: }
194:
195: public Collection getChildPagesWithFallback() {
196: return node.getChildPagesWithFallback(site);
197: }
198:
199: public Collection getAncestors() {
200: LinkedList pages = new LinkedList();
201: Page page = this ;
202: while (page != null) {
203: pages.addFirst(page);
204: page = page.getParentPage();
205: }
206: return pages;
207: }
208:
209: public void addChildPage(Page child) {
210: child.setSite(site);
211: node.addChildNode(new PageNode(child));
212: }
213:
214: public String getHandlerName() {
215: return node.getHandlerName();
216: }
217:
218: public VersionContainer getVersionContainer() {
219: if (versionContainer == null) {
220: versionContainer = new VersionContainer();
221: ComponentVersion version = new ComponentVersion(Page.class
222: .getName());
223: versionContainer.setLiveVersion(version);
224: }
225: return versionContainer;
226: }
227:
228: public void setVersionContainer(VersionContainer versionContainer) {
229: this .versionContainer = versionContainer;
230: }
231:
232: public ComponentVersion getComponentVersion(boolean preview) {
233: return preview ? getVersionContainer().getLatestVersion()
234: : getVersionContainer().getLiveVersion();
235: }
236:
237: public Map getProperties(boolean preview) {
238: ComponentVersion version = getComponentVersion(preview);
239: return version != null ? version.getProperties() : null;
240: }
241:
242: public String getProperty(String key, boolean preview) {
243: ComponentVersion version = getComponentVersion(preview);
244: return version != null ? version.getProperty(key) : null;
245: }
246:
247: public String getTitle(boolean preview) {
248: String title = getProperty(TITLE_PROPERTY, preview);
249: if (title == null) {
250: title = FormatUtils.xmlToTitleCase(pathComponent);
251: }
252: return title;
253: }
254:
255: public boolean isDirty() {
256: return getVersionContainer().isDirty();
257: }
258:
259: public boolean isPublished() {
260: return this .published;
261: }
262:
263: public void setPublished(boolean published) {
264: this .published = published;
265: }
266:
267: public boolean isEnabled() {
268: return published && site.isEnabled();
269: }
270:
271: public boolean isVisible(boolean preview) {
272: return !isHidden() && !node.isHidden() && !isWildcard()
273: && (isEnabled() || preview);
274: }
275:
276: public String toString() {
277: return site + ":" + path;
278: }
279:
280: public boolean equals(Object o) {
281: if (o instanceof Page) {
282: Page page = (Page) o;
283: if (id == null) {
284: return this == o;
285: }
286: return id.equals(page.getId());
287: }
288: return false;
289: }
290:
291: public int hashCode() {
292: return id != null ? id.hashCode() : 0;
293: }
294:
295: }
|