KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > template > DefaultModelHelper


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.repo.component.template;
18
19 import java.util.Date JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.repo.template.DateCompareMethod;
24 import org.alfresco.repo.template.HasAspectMethod;
25 import org.alfresco.repo.template.I18NMessageMethod;
26 import org.alfresco.service.ServiceRegistry;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.TemplateImageResolver;
29 import org.alfresco.service.cmr.repository.TemplateNode;
30 import org.alfresco.web.app.Application;
31 import org.alfresco.web.bean.repository.Repository;
32 import org.alfresco.web.bean.repository.User;
33 import org.alfresco.web.ui.common.Utils;
34
35 /**
36  * Helper class to generate the default template model.
37  * <p>
38  * See {@link http://www.alfresco.org/mediawiki/index.php/Template_Guide}
39  *
40  * @author Kevin Roast
41  */

42 public class DefaultModelHelper
43 {
44    /**
45     * Private Constructor
46     */

47    private DefaultModelHelper()
48    {
49    }
50
51    /**
52     * Construct the default FreeMarker template model.
53     * <p>
54     * Other root level objects such as the current Space or Document are generally
55     * added by the appropriate bean responsible for provided access to those nodes.
56     * <p>
57     * See {@link http://www.alfresco.org/mediawiki/index.php/Template_Guide}
58     *
59     * @return Map containing the default model.
60     */

61    public static Map JavaDoc buildDefaultModel(ServiceRegistry services, User user)
62    {
63       if (services == null)
64       {
65          throw new IllegalArgumentException JavaDoc("ServiceRegistry is mandatory.");
66       }
67       if (user == null)
68       {
69          throw new IllegalArgumentException JavaDoc("Current User is mandatory.");
70       }
71       
72       // create FreeMarker default model and merge
73
Map JavaDoc root = new HashMap JavaDoc(16, 1.0f);
74       
75       // supply the CompanyHome space as "companyhome"
76
NodeRef companyRootRef = new NodeRef(Repository.getStoreRef(), Application.getCompanyRootId());
77       TemplateNode companyRootNode = new TemplateNode(companyRootRef, services, imageResolver);
78       root.put("companyhome", companyRootNode);
79       
80       // supply the users Home Space as "userhome"
81
NodeRef userRootRef = new NodeRef(Repository.getStoreRef(), user.getHomeSpaceId());
82       TemplateNode userRootNode = new TemplateNode(userRootRef, services, imageResolver);
83       root.put("userhome", userRootNode);
84       
85       // supply the current user Node as "person"
86
root.put("person", new TemplateNode(user.getPerson(), services, imageResolver));
87       
88       // current date/time is useful to have and isn't supplied by FreeMarker by default
89
root.put("date", new Date JavaDoc());
90       
91       // add custom method objects
92
root.put("hasAspect", new HasAspectMethod());
93       root.put("message", new I18NMessageMethod());
94       root.put("dateCompare", new DateCompareMethod());
95       
96       return root;
97    }
98    
99    /** Template Image resolver helper */
100    public static TemplateImageResolver imageResolver = new TemplateImageResolver()
101    {
102        public String JavaDoc resolveImagePathForName(String JavaDoc filename, boolean small)
103        {
104            return Utils.getFileTypeImage(filename, small);
105        }
106    };
107 }
108
Popular Tags