001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.admin.ui.actions;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.core.model.portal.Page;
026: import org.jboss.portal.core.model.portal.PageContainer;
027:
028: import javax.faces.application.FacesMessage;
029: import javax.faces.context.FacesContext;
030:
031: /**
032: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
033: * @version $Revision: 8785 $
034: */
035: public class AddPageAction {
036:
037: /** . */
038: private static final Logger log = Logger
039: .getLogger(AddPageAction.class);
040:
041: /** . */
042: private static final String ERROR_DUPE_NAME = "Duplicate Page name found on this portal!";
043:
044: /** . */
045: private static final String ERROR_EMPTY_NAME = "Page name cannot be blank!";
046:
047: /** . */
048: private static final String ERROR_NO_NAME = "Page name not specified!";
049:
050: /** . */
051: private static final String ERROR_INTERNAL_ERROR = "An internal error happened!";
052:
053: /** The page container that will create the child. */
054: public PageContainer pageContainer;
055:
056: /** The page name to create. */
057: public String pageName;
058:
059: /** An optional listener. */
060: public Listener listener;
061:
062: /** The message id when an error happens. */
063: public String messageTarget;
064:
065: public void execute() {
066: FacesMessage message = null;
067:
068: //
069: if (pageName == null) {
070: message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
071: ERROR_NO_NAME, ERROR_NO_NAME);
072: } else {
073: pageName = pageName.trim();
074:
075: //
076: if (pageName.length() == 0) {
077: message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
078: ERROR_EMPTY_NAME, ERROR_EMPTY_NAME);
079: } else if (pageContainer.getChild(pageName) != null) {
080: message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
081: ERROR_DUPE_NAME, ERROR_DUPE_NAME);
082: }
083: }
084:
085: //
086: if (message == null) {
087: try {
088: Page page = pageContainer.createPage(pageName);
089:
090: //
091: if (listener != null) {
092: listener.pageCreated(page);
093: }
094: } catch (Exception e) {
095: log.error("An error happened during page creation", e);
096: message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
097: ERROR_INTERNAL_ERROR, ERROR_INTERNAL_ERROR);
098: }
099: }
100:
101: //
102: if (message != null && messageTarget != null) {
103: FacesContext.getCurrentInstance().addMessage(messageTarget,
104: message);
105: }
106: }
107:
108: public interface Listener {
109: void pageCreated(Page page);
110: }
111:
112: public PageContainer getPageContainer() {
113: return pageContainer;
114: }
115:
116: public void setPageContainer(PageContainer pageContainer) {
117: this .pageContainer = pageContainer;
118: }
119:
120: public String getMessageTarget() {
121: return messageTarget;
122: }
123:
124: public void setMessageTarget(String messageTarget) {
125: this .messageTarget = messageTarget;
126: }
127:
128: public Listener getListener() {
129: return listener;
130: }
131:
132: public void setListener(Listener listener) {
133: this.listener = listener;
134: }
135: }
|