View Javadoc
1   /*
2    * Copyright (C) 2003-2011 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.wiki.mow.core.api.wiki;
18  
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.Map.Entry;
22  
23  import org.chromattic.api.annotations.Create;
24  import org.chromattic.api.annotations.OneToMany;
25  import org.chromattic.api.annotations.Path;
26  import org.chromattic.api.annotations.PrimaryType;
27  import org.exoplatform.services.log.ExoLogger;
28  import org.exoplatform.services.log.Log;
29  import org.exoplatform.wiki.WikiException;
30  
31  @PrimaryType(name = WikiNodeType.WIKI_TEMPLATE_CONTAINER)
32  public abstract class TemplateContainer {
33  
34    @Create
35    public abstract TemplateImpl createTemplatePage();
36    
37    @Path
38    public abstract String getPath();
39  
40    @OneToMany
41    public abstract Map<String, TemplateImpl> getTemplates();
42    public abstract void setTemplates(Map<String,TemplateImpl> templates);
43    
44    public TemplateImpl addPage(String templateName, TemplateImpl template) {
45      if (templateName == null) {
46        throw new IllegalArgumentException("Template name cannot be null");
47      }
48      if (template == null) {
49        throw new IllegalArgumentException("Template cannot be null");
50      }
51      Map<String, TemplateImpl> children = getTemplates();
52      if (children.containsKey(templateName)) {
53        return template;
54      }
55      children.put(templateName, template);
56  
57      return template;
58    }
59    
60    public TemplateImpl getTemplate(String name) {
61      Iterator<Entry<String, TemplateImpl>> iter = getTemplates().entrySet().iterator();
62      while (iter.hasNext()) {
63        Entry<String, TemplateImpl> entry = iter.next();
64        TemplateImpl template = entry.getValue();
65        if (template.getName().equals(name)) {
66          return template;
67        }
68      }
69      return null;
70    };  
71  
72  }