View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.render.bean;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  
24  import org.springframework.extensions.surf.RequestContext;
25  import org.springframework.extensions.surf.WebFrameworkConstants;
26  import org.springframework.extensions.surf.render.Processor;
27  import org.springframework.extensions.surf.render.ProcessorContext;
28  import org.springframework.extensions.surf.render.RenderContext;
29  import org.springframework.extensions.surf.render.RenderMode;
30  import org.springframework.extensions.surf.render.Renderable;
31  import org.springframework.extensions.surf.render.ProcessorContext.ProcessorDescriptor;
32  import org.springframework.extensions.surf.render.bean.ComponentRenderer;
33  import org.springframework.extensions.surf.studio.WebStudioService;
34  import org.springframework.extensions.surf.types.Component;
35  import org.springframework.extensions.surf.types.ComponentType;
36  import org.springframework.extensions.webscripts.DeclarativeRegistry;
37  import org.springframework.extensions.webscripts.WebScript;
38  
39  /**
40   * Modifies the way that components are rendered when Surf is running in preview mode.
41   * This allows Web Studio to pull "pulled in" to any Surf Preview application.
42   * 
43   * Primarily, this enables components to output additional Web Studio
44   * specific JavaScript to bind client-side DOM elements together.
45   * 
46   * @author muzquiano
47   */
48  public class WebStudioComponentRenderer extends ComponentRenderer
49  {
50      private WebStudioService webStudioService = null;
51      private DeclarativeRegistry registry = null;
52      
53      /**
54       * Sets the web studio service.
55       * 
56       * @param webStudioService the new web studio service
57       */
58      public void setWebStudioService(WebStudioService webStudioService)
59      {
60          this.webStudioService = webStudioService;
61      }
62      
63      /**
64       * Sets the registry.
65       * 
66       * @param registry the new registry
67       */
68      public void setRegistry(DeclarativeRegistry registry)
69      {
70          this.registry = registry;
71      }
72      
73      /**
74       * Gets the web studio service.
75       * 
76       * @return the web studio service
77       */
78      protected WebStudioService getWebStudioService()
79      {
80          return this.webStudioService;
81      }
82      
83      /*
84       * (non-Javadoc)
85       * 
86       * @see org.alfresco.web.framework.render.bean.RegionRenderer#postProcess(org.alfresco.web.framework.render.RenderContext)
87       */
88      public void postProcess(RenderContext context) throws IOException
89      {
90          // TODO: check whether web studio is enabled
91          boolean webStudioEnabled = true;
92          if (webStudioEnabled)
93          {
94              // if we are not in passive mode
95              if (!context.isPassiveMode())
96              {
97                  // html binding id
98                  String htmlId = (String) context
99                          .getValue(WebFrameworkConstants.RENDER_DATA_HTMLID);
100     
101                 // component and component type properties
102                 String componentId = (String) context
103                         .getValue(WebFrameworkConstants.RENDER_DATA_COMPONENT_ID);
104                 String componentTypeId = (String) context
105                         .getValue(WebFrameworkConstants.RENDER_DATA_COMPONENT_TYPE_ID);
106     
107                 // commit to output
108                 PrintWriter writer = context.getResponse().getWriter();
109                 writer.println("<script type='text/javascript'>");
110     
111                 // if there is a component, bind that in too
112                 if (componentId != null && componentTypeId != null)
113                 {
114                     String componentTitle = "";
115                     String componentTypeTitle = "";
116                     
117                     Component c = (Component) context.getModel().getComponent(componentId);
118                     if (c != null)
119                     {
120                         componentTitle = getComponentTitle(context, c);
121                         componentTypeTitle = getComponentTypeTitle(context, c);
122                     }
123                     
124                     writer.println("if(typeof WebStudio != \"undefined\"){");
125                     writer.println("WebStudio.configureComponent('" + htmlId
126                             + "', '" + componentId + "', '" + componentTypeId
127                             + "', '" + componentTitle + "', '" + componentTypeTitle + "');");
128                     
129                     // if the component has an "edit" mode, then mark it as editable
130                     boolean hasEditMode = false;
131                     ComponentType ct = (ComponentType) c.getComponentType(context);
132                     if (ct instanceof Renderable)
133                     {
134                         Renderable renderable = (Renderable) ct;
135                         
136                         // get the processor
137                         Processor processor = this.getRenderService().getProcessor((Renderable)ct, RenderMode.EDIT);
138                         if (processor != null)
139                         {
140                             // build a mock render context
141                             RenderContext renderContext = getRenderService().provideRenderContext(context, context.getRequest(), context.getResponse(), RenderMode.EDIT);
142                             
143                             // build a processor context
144                             ProcessorContext processorContext = new ProcessorContext(renderContext);
145                             
146                             // load from renderable data
147                             processorContext.load((Renderable)ct);
148                             
149                             // TODO: we have to manually set up view mode for possible substitutions in other modes
150                             if (c.getURL() != null)
151                             {
152                                 ProcessorDescriptor viewDescriptor = processorContext.getDescriptor(RenderMode.VIEW);
153                                 viewDescriptor.put("uri", c.getURL());
154                             }
155                             
156                             hasEditMode = processor.exists(processorContext);
157                         }
158                     }
159                     
160                     if (hasEditMode)
161                     {
162                         writer.println("WebStudio.configureComponentEditable('" + htmlId + "');");
163                     }
164                     
165                     
166                     writer.println("}");
167                 }
168     
169                 writer.println("</script>");
170                 writer.flush();
171             }
172         }
173     }
174 
175     public String getComponentTitle(RequestContext context, Component c)
176     {
177         String title = c.getTitle();
178         if (title == null)
179         {
180             title = c.getId();
181         }
182 
183         return title;
184     }
185 
186     public String getComponentTypeTitle(RequestContext context,
187             Component c)
188     {
189         String title = null;
190 
191         ComponentType ct = c.getComponentType(context);
192         if (ct != null)
193         {
194             title = ct.getTitle();
195             if (title == null)
196             {
197                 title = ct.getId();
198             }
199 
200             if ("webscript".equals(ct.getId()))
201             {
202                 String url = c.getURL();
203 
204                 WebScript webScript = this.registry.getWebScript(url);
205                 if (webScript != null)
206                 {
207                     title = webScript.getDescription().getShortName();
208                 }
209             }
210         }
211         else
212         {
213             // assume the component type field is a web script id
214             String url = c.getComponentTypeId();
215 
216             for (WebScript webscript : this.registry.getWebScripts())
217             {
218                 String[] uris = webscript.getDescription().getURIs();
219                 for (int z = 0; z < uris.length; z++)
220                 {
221                     String uri = uris[z];
222                     if (uri.equals(url))
223                     {
224                         title = webscript.getDescription().getShortName();
225                     }
226                 }
227             }
228         }
229 
230         return title;
231     }
232 }