KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > settings > ConfigModule


1 /*
2  * Created on Jan 5, 2005
3  */

4 package com.openedit.modules.settings;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.openedit.repository.filesystem.StringItem;
12
13 import com.openedit.OpenEditException;
14 import com.openedit.WebPageRequest;
15 import com.openedit.config.Configuration;
16 import com.openedit.modules.edit.BaseEditorModule;
17 import com.openedit.modules.translations.Language;
18 import com.openedit.modules.translations.Translation;
19 import com.openedit.page.FileFinder;
20 import com.openedit.page.Page;
21 import com.openedit.page.PageSettings;
22 import com.openedit.page.XconfConfiguration;
23 import com.openedit.util.PathUtilities;
24
25 /**
26  * @author cburkey
27  *
28  */

29 public class ConfigModule extends BaseEditorModule
30 {
31     private static final Log log = LogFactory.getLog(ConfigModule.class);
32     public void readConfig(WebPageRequest inReq) throws OpenEditException
33     {
34         try
35         {
36             //The GUI needs to send us in the path to the xconf file somehow
37
String JavaDoc path = inReq.getRequestParameter("editPath");
38             if( path == null)
39             {
40                 return;
41             }
42             String JavaDoc origURL = inReq.getRequestParameter("origURL");
43             if( origURL == null)
44             {
45                 log.info("Reloading");
46                 return;
47             }
48             Page webPage = getPageManager().getPage(path);
49             String JavaDoc editPath = webPage.getPageSettings().getXConf().getPath(); //get .xconf path
50

51             String JavaDoc draftPath = PathUtilities.createDraftPath(editPath);
52             Page draft = getPageManager().getPage(draftPath);
53             if( draft.exists() )
54             {
55                 editPath = draftPath;
56             }
57
58             Page editPage = getPageManager().getPage(editPath);
59
60             XconfConfiguration config = new XconfConfiguration();
61             //find the correct xconf for this type
62
PageSettings settings = editPage.getPageSettings();
63
64             if( settings.exists() )
65             {
66                 config.readXML(settings.getReader());
67             }
68             else
69             {
70                 config.setName("page");
71             }
72             ConfigEditorSession session = (ConfigEditorSession)inReq.getSessionValue("configeditsession");
73             String JavaDoc windowname = inReq.getRequestParameter("parentName");
74             if( windowname == null && session != null && session.getEditPath() == editPath )
75             {
76                 //reload the data and return
77
session.setConfig(config);
78                 return;
79             }
80             
81             session = new ConfigEditorSession();
82             session.setConfig(config);
83             
84             session.setEditPage(editPage);
85             session.setParentName(windowname);
86             
87             FileFinder finder = new FileFinder();
88             finder.setRoot(getRoot());
89             finder.setPageManager(getPageManager());
90             List JavaDoc layouts = finder.findPages("layout");
91             session.setLayouts(layouts);
92
93             session.setOriginalUrl(origURL);
94
95             inReq.putSessionValue("configeditsession", session);
96             
97             Translation trans = (Translation) inReq.getPageValue("translations");
98             if( trans != null)
99             {
100                 for (Iterator JavaDoc iter = config.getAllProperties().iterator(); iter.hasNext();)
101                 {
102                     String JavaDoc id = (String JavaDoc) iter.next();
103                     Configuration propconfig = config.getProperty(id);
104                     if( propconfig != null)
105                     {
106                         for (Iterator JavaDoc iterator = propconfig.getChildIterator("value"); iterator.hasNext();)
107                         {
108                             Configuration val = (Configuration) iterator.next();
109                             String JavaDoc local = val.getAttribute("locale");
110                             if( local != null)
111                             {
112                                 Language lang = trans.getLanguage(local);
113                                 if ( lang == null)
114                                 {
115                                     lang = new Language();
116                                     lang.setName(local);
117                                     lang.setId(local);
118                                     trans.addLanguage(lang);
119                                 }
120                             }
121                         }
122                     }
123                 }
124             }
125             
126         }
127         catch (Throwable JavaDoc ex)
128         {
129             throw new OpenEditException(ex);
130         }
131     }
132     public void saveConfigChanges(WebPageRequest inReq) throws Exception JavaDoc
133     {
134         //they already selected the Xconf from a menu
135
ConfigEditorSession session = (ConfigEditorSession)inReq.getSessionValue("configeditsession");
136         XconfConfiguration config = session.getConfig();
137         
138         HtmlToXconfReader converter = new HtmlToXconfReader();
139         converter.setAdvancedMode(inReq.getUser().hasPermission("oe.edit.settings.advanced"));
140         converter.saveChangesToConfig(inReq, config );
141         
142         //If there was no config there before and its still empty then dont bother saving a new config
143
if ( config.isEmpty())
144         {
145             //delete it
146
getPageManager().removePage(session.getEditPage()); //delete xconf
147
}
148         else
149         {
150             
151             String JavaDoc xml = config.toXml(session.getEditPage().getCharacterEncoding());
152             StringItem content = new StringItem(
153                 session.getEditPath(), xml ,session.getEditPage().getCharacterEncoding());
154             content.setAuthor(inReq.getUser().getUserName());
155             content.setMessage("Edited settings");
156             getPageManager().getPageSettingsManager().saveSetting(content);
157         }
158     }
159
160 }
161
Popular Tags