001: package org.contineo.web.document;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005:
006: import org.contineo.core.document.Article;
007: import org.contineo.core.document.Document;
008: import org.contineo.core.document.dao.ArticleDAO;
009: import org.contineo.core.document.dao.DocumentDAO;
010:
011: import org.contineo.util.Context;
012:
013: import org.contineo.web.SessionManagement;
014: import org.contineo.web.i18n.Messages;
015: import org.contineo.web.navigation.PageContentBean;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Date;
020:
021: import javax.faces.application.FacesMessage;
022:
023: /**
024: * Control that allows the user to list and select articles
025: *
026: * @author Marco Meschieri
027: * @version $Id: ArticlesRecordsManager.java,v 1.3 2006/09/03 16:24:37 marco Exp $
028: * @since 3.0
029: */
030: public class ArticlesRecordsManager {
031: protected static Log log = LogFactory
032: .getLog(ArticlesRecordsManager.class);
033: private Collection<Article> articles = new ArrayList<Article>();
034: private ArticleRecord selectedArticle;
035: private Document selectedDocument;
036: private boolean editing = false;
037: private DocumentNavigation documentNavigation;
038:
039: public boolean isEditing() {
040: return editing;
041: }
042:
043: /**
044: * Changes the currently selected document and updates the articles list.
045: *
046: * @param doc
047: */
048: public void selectDocument(Document doc) {
049: selectedDocument = doc;
050:
051: // initiate the list
052: if (articles != null) {
053: articles.clear();
054: } else {
055: articles = new ArrayList<Article>(10);
056: }
057:
058: try {
059: int menuId = selectedDocument.getMenuId();
060: int docId = selectedDocument.getDocId();
061:
062: DocumentDAO docDao = (DocumentDAO) Context.getInstance()
063: .getBean(DocumentDAO.class);
064: Document document = docDao.findByMenuId(menuId);
065: docId = document.getDocId();
066:
067: ArticleDAO artDao = (ArticleDAO) Context.getInstance()
068: .getBean(ArticleDAO.class);
069: Collection<Article> coll = artDao.findByDocId(docId);
070:
071: for (Article article : coll) {
072: articles.add(new ArticleRecord(article, this ));
073: }
074: } catch (Exception e) {
075: log.error(e.getMessage(), e);
076: Messages.addMessage(FacesMessage.SEVERITY_ERROR, e
077: .getMessage(), e.getMessage());
078: }
079: }
080:
081: /**
082: * Cleans up the resources used by this class. This method could be called
083: * when a session destroyed event is called.
084: */
085: public void dispose() {
086: articles.clear();
087: }
088:
089: /**
090: * Gets the list of articles which will be used by the ice:dataTable
091: * component.
092: *
093: * @return array list of articles
094: */
095: public Collection<Article> getArticles() {
096: return articles;
097: }
098:
099: public String back() {
100: if (selectedArticle == null) {
101: documentNavigation.setSelectedPanel(new PageContentBean(
102: "documents"));
103: } else {
104: selectedArticle = null;
105: }
106:
107: editing = false;
108:
109: return null;
110: }
111:
112: public String save() {
113: if (SessionManagement.isValid()) {
114: try {
115: int docId = selectedDocument.getDocId();
116: String username = SessionManagement.getUsername();
117: selectedArticle.setArticleDate(String
118: .valueOf(new Date().getTime()));
119: selectedArticle.setUsername(username);
120: selectedArticle.setDocId(docId);
121:
122: ArticleDAO articleDao = (ArticleDAO) Context
123: .getInstance().getBean(ArticleDAO.class);
124: articleDao.store(selectedArticle.getWrappedArticle());
125:
126: Messages.addLocalizedInfo("msg.action.savearticle");
127: } catch (Exception e) {
128: log.error(e.getMessage(), e);
129: Messages.addLocalizedError("errors.action.savearticle");
130: }
131:
132: selectDocument(selectedDocument);
133: selectedArticle = null;
134: editing = false;
135:
136: return null;
137: } else {
138: return "login";
139: }
140: }
141:
142: public String add() {
143: editing = true;
144: selectedArticle = new ArticleRecord(new Article(), this );
145: selectedArticle.setUsername(SessionManagement.getUsername());
146:
147: return null;
148: }
149:
150: public Document getSelectedDocument() {
151: return selectedDocument;
152: }
153:
154: public void setSelectedDocument(Document selectedDocument) {
155: this .selectedDocument = selectedDocument;
156: }
157:
158: public ArticleRecord getSelectedArticle() {
159: return selectedArticle;
160: }
161:
162: public void setSelectedArticle(ArticleRecord selectedArticle) {
163: this .selectedArticle = selectedArticle;
164: }
165:
166: public void setDocumentNavigation(
167: DocumentNavigation documentNavigation) {
168: this.documentNavigation = documentNavigation;
169: }
170: }
|