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.webscripts;
20  
21  import java.io.Serializable;
22  
23  import org.springframework.extensions.surf.ModelObject;
24  import org.springframework.extensions.surf.RequestContext;
25  import org.springframework.extensions.surf.resource.Resource;
26  import org.springframework.extensions.surf.resource.ResourceProvider;
27  import org.springframework.extensions.webscripts.ScriptableLinkedHashMap;
28  import org.springframework.extensions.webscripts.ScriptableMap;
29  
30  /**
31   * Provides an interface to resources as stored on a model object.
32   * 
33   * var objectId = resources.get("abc").objectId;
34   * var endpointId = resources.get("abc").endpointId;
35   * 
36   * @author muzquiano
37   */
38  public final class ScriptResources extends ScriptBase
39  {
40      private static final long serialVersionUID = -3378946227712931201L;
41      
42      final private ModelObject modelObject;
43          
44      /**
45       * Instantiates a new resources object
46       * 
47       * @param context the request context
48       * @param modelObject the model object
49       */
50      public ScriptResources(RequestContext context, ModelObject modelObject)
51      {
52          super(context);
53  
54          this.modelObject = modelObject;
55      }
56      
57      /* (non-Javadoc)
58       * @see org.alfresco.web.scripts.AbstractScriptableObject#buildProperties()
59       */
60      protected ScriptableMap buildProperties()
61      {
62          if (this.properties == null)
63          {
64              // construct and add in all of our model object properties
65              this.properties = new ScriptableLinkedHashMap<String, Serializable>()
66              {
67                  // For now, leave this as a read-only map
68              };
69              
70              // populate the scriptable map
71              String[] resourceNames = this.getNames();
72              for (int i = 0; i < resourceNames.length; i++)
73              {
74                  ScriptResource scriptResource = this.get(resourceNames[i]);
75                  this.properties.put(resourceNames[i], scriptResource);
76              }
77          }
78          
79          return this.properties;
80  
81      }
82      
83      
84      // --------------------------------------------------------------
85      // JavaScript Functions
86          
87      /**
88       * Returns the model object
89       * 
90       * @return
91       */
92      public ModelObject getModelObject()
93      {
94          return this.modelObject;
95      }
96      
97      public ScriptResource get(String name)
98      {
99          ScriptResource scriptResource = null;
100         
101         if (modelObject instanceof ResourceProvider)
102         {
103             ResourceProvider provider = (ResourceProvider) modelObject;
104             
105             // now add
106             Resource resource = provider.getResource(name);
107             if (resource != null)
108             {
109                 scriptResource = new ScriptResource(context, resource);
110             }
111         }
112         
113         return scriptResource;        
114     }
115     
116     public void remove(String name)
117     {
118         if (modelObject instanceof ResourceProvider)
119         {
120             ResourceProvider provider = (ResourceProvider) modelObject;                        
121             provider.removeResource(name);
122             
123             // update properties
124             this.properties.remove(name);
125         }
126     }
127     
128     public ScriptResource add(String name, String resourceId)
129     {
130         ScriptResource scriptResource = null;
131         
132         if (modelObject instanceof ResourceProvider)
133         {
134             ResourceProvider provider = (ResourceProvider) modelObject;                        
135             
136             // now add
137             Resource resource = provider.addResource(name, resourceId);
138             if (resource != null)
139             {
140                 scriptResource = new ScriptResource(context, resource);
141                 this.properties.put(name, scriptResource);
142             }
143         }
144         
145         return scriptResource;
146     }
147 
148     public ScriptResource add(String name, String protocolId, String endpointId, String objectId)
149     {
150         ScriptResource scriptResource = null;
151         
152         if (modelObject instanceof ResourceProvider)
153         {
154             ResourceProvider provider = (ResourceProvider) modelObject;                        
155             
156             // now add
157             Resource resource = provider.addResource(name, protocolId, endpointId, objectId);
158             if (resource != null)
159             {
160                 scriptResource = new ScriptResource(context, resource);
161                 this.properties.put(name, scriptResource);
162             }
163         }
164         
165         return scriptResource;
166     }
167     
168     public String[] getNames()
169     {
170         String[] names = new String[0];
171         
172         if (modelObject instanceof ResourceProvider)
173         {        
174             ResourceProvider provider = (ResourceProvider) modelObject;        
175             Resource[] array = provider.getResources();
176             if (array.length > 0)
177             {
178                 names = new String[array.length];
179                 
180                 for (int i = 0; i < array.length; i++)
181                 {
182                     names[i] = array[i].getName();
183                 }
184             }
185         }
186         
187         return names;
188     }
189 }