View Javadoc

1   /*
2    * Copyright 2003,2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.portlet.admin.model;
17  
18  import java.io.FileReader;
19  import java.io.FileWriter;
20  import java.util.Collection;
21  import java.util.Iterator;
22  
23  import org.apache.pluto.portalImpl.om.page.impl.FragmentImpl;
24  import org.apache.pluto.portalImpl.om.page.impl.PortalImpl;
25  import org.apache.pluto.portlet.admin.BaseAdminObject;
26  import org.apache.pluto.portlet.admin.PlutoAdminException;
27  import org.apache.pluto.portlet.admin.util.PlutoAdminContext;
28  import org.exolab.castor.mapping.Mapping;
29  import org.exolab.castor.xml.Marshaller;
30  import org.exolab.castor.xml.Unmarshaller;
31  
32  /***
33   *
34   * This class is used to access and store data in the pageregistry.xml file.
35   * It uses Castor and is loosely based on the
36   * org.apache.services.pageregistry.PageRegistryServiceFileImpl
37   * class in Pluto's 'portal' module.
38   *
39   * @author Craig Doremus
40   */
41  public class PageRegistryXao extends BaseAdminObject {
42  
43  
44      // default configuration values
45      public final static String CONFIG_FILE              = "WEB-INF/data/pageregistry.xml";
46      public final static String DEFAULT_MAPPING          = "WEB-INF/data/xml/pageregistrymapping.xml";
47      private final static String CLASS_NAME = "PageRegistryXao";
48      // Castor mapping file
49      private Mapping mapping = null;
50  
51  	/***
52  	 *
53  	 */
54  	public PageRegistryXao() throws Exception {
55  		super(CLASS_NAME);
56  		init();
57  	}
58      public void init () throws Exception
59      {
60      	 final String METHOD_NAME = "init()";
61  
62          String _mapping = PlutoAdminContext.getInstance().getPlutoHome() + "/" + DEFAULT_MAPPING;
63  
64          this.mapping = new Mapping();
65          try
66          {
67              this.mapping.loadMapping(_mapping);
68          }
69          catch (Exception e)
70          {
71              logError(METHOD_NAME, "Failed to load mapping file "+_mapping,e);
72              throw e;
73          }
74  
75      }
76  
77  
78      public void save(PortalImpl page) throws Exception {
79      	final String METHOD_NAME = "save(PortalImpl)";
80        String filename = PlutoAdminContext.getInstance().getPlutoHome() + "/" + CONFIG_FILE;
81      	logDebug(METHOD_NAME, "Registry file to save: " + filename);
82  
83          FileWriter writer = new FileWriter(filename);
84  
85          Marshaller marshaller = new Marshaller(writer);
86  
87          marshaller.setMapping(this.mapping);
88  
89          marshaller.marshal(page);
90      }
91  
92      public PortalImpl load() throws Exception
93      {
94      	final String METHOD_NAME = "load()";
95  
96          String filename = PlutoAdminContext.getInstance().getPlutoHome() + "/" + CONFIG_FILE;
97        	logDebug(METHOD_NAME, "File to load: " + filename);
98  
99          Unmarshaller unmarshaller = new Unmarshaller(this.mapping);
100         unmarshaller.setMapping(this.mapping);
101 
102         PortalImpl pages = (PortalImpl)unmarshaller.unmarshal(new FileReader(filename));
103         return pages;
104     }
105 
106     public boolean pageExists(String page) {
107     	final String METHOD_NAME = "pageExists(page)";
108     	boolean exists = false;
109     	Collection frags = null;
110   		try {
111       	PortalImpl portal = load();
112       	frags = portal.getFragments();
113 			} catch (Exception e) {
114 	  		logError(METHOD_NAME, e);
115 				throw new PlutoAdminException(e);
116 			}
117 			Iterator iter = frags.iterator();
118     	while (iter.hasNext()) {
119     		FragmentImpl frag = (FragmentImpl) iter.next();
120     		String type = null;
121     		String name = null;
122     		if (frag != null) {
123 				type = frag.getType();
124 				name = frag.getName();
125 			}
126     		if(type != null && type.equalsIgnoreCase("page") && name != null && name.equalsIgnoreCase(page)) {
127     			exists = true;
128     			break;
129     		}
130     	}
131     	return exists;
132     }
133 }