View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.component.admin.views;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.MissingResourceException;
22  import java.util.ResourceBundle;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.exoplatform.services.cms.views.ManageViewService;
26  import org.exoplatform.services.log.ExoLogger;
27  import org.exoplatform.services.log.Log;
28  import org.exoplatform.services.wcm.core.NodetypeConstant;
29  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
30  import org.exoplatform.webui.application.WebuiRequestContext;
31  import org.exoplatform.webui.config.annotation.ComponentConfig;
32  import org.exoplatform.webui.config.annotation.ComponentConfigs;
33  import org.exoplatform.webui.config.annotation.EventConfig;
34  import org.exoplatform.webui.core.UIComponent;
35  import org.exoplatform.webui.core.UIPopupWindow;
36  import org.exoplatform.webui.core.UITabPane;
37  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
38  import org.exoplatform.webui.event.Event;
39  import org.exoplatform.webui.event.Event.Phase;
40  import org.exoplatform.webui.event.EventListener;
41  import org.exoplatform.webui.form.UIForm;
42  import org.exoplatform.webui.form.UIFormInputBase;
43  
44  import javax.jcr.Node;
45  
46  /**
47   * Created by The eXo Platform SARL
48   * Author : Tran The Trong
49   *          trongtt@exoplatform.com
50   * Sep 19, 2006
51   * 5:31:04 PM
52   */
53  @ComponentConfigs({
54          @ComponentConfig(
55                  type = UIViewForm.class,
56                  lifecycle = UIFormLifecycle.class,
57                  template = "app:/groovy/webui/component/admin/view/UIForm.gtmpl",
58                  events = {
59                          @EventConfig(listeners = UIViewFormTabPane.SaveActionListener.class),
60                          @EventConfig(listeners = UIViewFormTabPane.RestoreActionListener.class, phase = Phase.DECODE),
61                          @EventConfig(listeners = UIViewFormTabPane.CancelActionListener.class, phase = Phase.DECODE),
62                          @EventConfig(listeners = UIViewFormTabPane.CloseActionListener.class, phase = Phase.DECODE),
63                          @EventConfig(listeners = UIViewFormTabPane.SelectTabActionListener.class, phase = Phase.DECODE),
64                          @EventConfig(listeners = UIViewForm.ChangeVersionActionListener.class, phase = Phase.DECODE)
65                  }),
66          @ComponentConfig(
67                  template =  "app:/groovy/webui/component/admin/view/UIViewFormTabPane.gtmpl"
68                  )
69                  
70  })
71  public class UIViewFormTabPane extends UITabPane {
72    private static final Log logger = ExoLogger.getLogger(UIViewFormTabPane.class.getName());
73    final static public String POPUP_PERMISSION = "PopupViewPermission" ;
74  
75    private String selectedTabId = "UITemplateContainer";
76  
77    public static final String SAVE_BUTTON    = "Save";
78    public static final String CANCEL_BUTTON  = "Cancel";
79    public static final String RESTORE_BUTTON = "Restore";
80    
81    private String[] actions_ = new String[] {SAVE_BUTTON, CANCEL_BUTTON};
82    private String primaryBtn_ = "Save";
83    
84    private boolean isUpdate_ = false;
85  
86    public String getSelectedTabId()
87    {
88       return selectedTabId;
89    }
90  
91    public void setSelectedTab(String renderTabId)
92    {
93       selectedTabId = renderTabId;
94    }
95  
96    public void setSelectedTab(int index)
97    {
98       selectedTabId = getChild(index - 1).getId();
99    }
100   
101   public String[] getActions() {
102     UITabList uiTabList = this.findFirstComponentOfType(UITabList.class);
103     String viewName = uiTabList.getViewName();
104     if(StringUtils.isNotEmpty(viewName) && isUpdate() ) {
105       try{
106         ManageViewService viewService = WCMCoreUtils.getService(ManageViewService.class);
107         Node viewNode = viewService.getViewByName(viewName, WCMCoreUtils.getSystemSessionProvider());
108         if (viewNode.isNodeType(NodetypeConstant.MIX_VERSIONABLE))
109           actions_ = new String[]{SAVE_BUTTON, CANCEL_BUTTON, RESTORE_BUTTON};
110       }catch (Exception ex){
111         logger.error("View {0} does not exits", viewName);
112       }
113     }
114     if(actions_.length == 1) primaryBtn_ = actions_[0];
115     return actions_;
116   }
117   
118   public void setActions(String[] actions) {
119     actions_ = actions;
120   }
121   
122   public String getPrimaryButtonAction() {
123     return primaryBtn_;
124   }
125   
126   public void setPrimaryButtonAction(String primaryBtn) {
127     primaryBtn_ = primaryBtn;
128   }  
129 
130   public UIViewFormTabPane() throws Exception {
131     UIViewForm uiViewForm = addChild(UIViewForm.class, null, null) ;
132     addChild(UITabContainer.class, null, null);
133     addChild(UIViewPermissionContainer.class, null, null);
134     setSelectedTab(uiViewForm.getId()) ;
135   }
136 
137   public String getLabel(ResourceBundle res, String id)  {
138     try {
139       return res.getString("UIViewForm.label." + id) ;
140     } catch (MissingResourceException ex) {
141       return id ;
142     }
143   }
144   
145   public void update(boolean isUpdate) {
146     isUpdate_ = isUpdate;
147     getChild(UIViewPermissionContainer.class).update(isUpdate);
148   }
149   
150   public void view(boolean isView) {
151     UITabContainer uiContainer = getChild(UITabContainer.class);
152     uiContainer.getChild(UITabList.class).view(isView);
153     getChild(UIViewPermissionContainer.class).view(isView);
154   }  
155   
156   public boolean isUpdate() {
157     return isUpdate_;
158   }
159 
160   static  public class SaveActionListener extends EventListener<UIViewForm> {
161     public void execute(Event<UIViewForm> event) throws Exception {
162       UIViewFormTabPane uiViewTabPane = event.getSource().getParent();
163       UIViewContainer uiViewContainer = uiViewTabPane.getAncestorOfType(UIViewContainer.class) ;
164       uiViewTabPane.getChild(UIViewForm.class).save() ;
165       UIPopupWindow uiPopup = null;
166       if(uiViewTabPane.isUpdate()) {
167         uiPopup = uiViewContainer.getChildById(UIViewList.ST_EDIT);
168       } else {
169         uiPopup = uiViewContainer.getChildById(UIViewList.ST_ADD);
170       }
171       uiPopup.setShow(false);
172       uiPopup.setRendered(false);
173       event.getRequestContext().addUIComponentToUpdateByAjax(uiViewContainer) ;
174     }
175   }
176 
177   static  public class CancelActionListener extends EventListener<UIViewForm> {
178     public void execute(Event<UIViewForm> event) throws Exception {
179       UIViewFormTabPane uiViewTabPane = event.getSource().getParent();
180       UIViewContainer uiViewContainer = uiViewTabPane.getAncestorOfType(UIViewContainer.class) ;
181       UIPopupWindow uiPopup = null;
182       if(uiViewTabPane.isUpdate()) {
183         uiPopup = uiViewContainer.getChildById(UIViewList.ST_EDIT);
184       } else {
185         uiPopup = uiViewContainer.getChildById(UIViewList.ST_ADD);
186       }
187       uiPopup.setShow(false);
188       uiPopup.setRendered(false);
189       event.getRequestContext().addUIComponentToUpdateByAjax(uiViewContainer) ;
190     }
191   }
192 
193   static  public class CloseActionListener extends EventListener<UIViewForm> {
194     public void execute(Event<UIViewForm> event) throws Exception {
195       UIViewFormTabPane uiViewTabPane = event.getSource().getParent();
196       UIViewContainer uiViewContainer = uiViewTabPane.getAncestorOfType(UIViewContainer.class) ;
197       UIPopupWindow uiPopup = uiViewContainer.getChildById(UIViewList.ST_VIEW);;
198       uiPopup.setShow(false);
199       uiPopup.setRendered(false);
200       event.getRequestContext().addUIComponentToUpdateByAjax(uiViewContainer) ;
201     }
202   }
203 
204   static  public class RestoreActionListener extends EventListener<UIViewForm> {
205     public void execute(Event<UIViewForm> event) throws Exception {
206       UIViewFormTabPane uiViewTabPane = event.getSource().getParent();
207       UIViewForm uiViewForm = uiViewTabPane.getChild(UIViewForm.class) ;
208       uiViewForm.changeVersion() ;
209       UIViewContainer uiContainer = uiViewTabPane.getAncestorOfType(UIViewContainer.class) ;
210       UIViewList uiViewList = uiContainer.findFirstComponentOfType(UIViewList.class) ;
211       uiViewList.refresh(uiViewList.getUIPageIterator().getCurrentPage());
212       uiViewForm.refresh(true) ;
213       uiViewTabPane.removeChildById(POPUP_PERMISSION) ;
214       UIViewContainer uiViewContainer = uiViewTabPane.getAncestorOfType(UIViewContainer.class) ;
215       uiViewContainer.removeChild(UIPopupWindow.class) ;
216       event.getRequestContext().addUIComponentToUpdateByAjax(uiViewContainer) ;
217     }
218   }
219 
220   static public class SelectTabActionListener extends EventListener<UIViewFormTabPane>
221   {
222     public void execute(Event<UIViewFormTabPane> event) throws Exception
223     {
224       WebuiRequestContext context = event.getRequestContext();
225       String renderTab = context.getRequestParameter(UIComponent.OBJECTID);
226       if (renderTab == null)
227         return;
228       event.getSource().setSelectedTab(renderTab);
229       WebuiRequestContext parentContext = (WebuiRequestContext)context.getParentAppRequestContext();
230       if (parentContext != null)
231       {
232         parentContext.setResponseComplete(true);
233       }
234       else
235       {
236         context.setResponseComplete(true);
237       }
238     }
239   } 
240   
241   public void processDecode(WebuiRequestContext context) throws Exception {
242     List<UIFormInputBase> inputs = new ArrayList<UIFormInputBase>();
243     this.findComponentOfType(inputs, UIFormInputBase.class);
244     String action = context.getRequestParameter(UIForm.ACTION);
245     for (UIFormInputBase input : inputs) {
246       if (!input.isValid()) {
247         continue;
248       }
249       String inputValue = context.getRequestParameter(input.getId());
250       if (inputValue == null || inputValue.trim().length() == 0) {
251         inputValue = context.getRequestParameter(input.getName());
252       }
253       input.decode(inputValue, context);
254     }
255     Event<UIComponent> event =  this.createEvent(action, Event.Phase.DECODE, context);
256     if (event != null) {
257       event.broadcast();
258     }
259   }
260   
261   public String event(String name) throws Exception {
262     StringBuilder b = new StringBuilder();
263     b.append("javascript:eXo.webui.UIForm.submitForm('").append("UIViewForm").append("','");
264     b.append(name).append("',true)");
265     return b.toString();
266   }  
267 }