📄 standardpagebuilder.java
字号:
package com.jdon.cms;
import java.util.*;
import java.io.*;
import com.jdon.cms.template.JspMaker;
import com.jdon.cms.PageBuilder;
import com.jdon.cms.Navlink;
import com.jdon.cms.Menu;
import com.jdon.cms.Body;
import com.jdon.cms.Page;
import com.jdon.cms.PageFactory;
import com.jdon.cms.events.PageEvent;
import com.jdon.util.Debug;
import com.jdon.cms.xml.XmlUtil;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Jdon.com Copyright (c) 2003</p>
* <p>Company: </p>
* @author banq
* @version 1.0
*/
public class StandardPageBuilder implements PageBuilder {
private final static String module = StandardPageBuilder.class.getName();
private final static XmlUtil xmlUtil = XmlUtil.getInstance();
private final static PageFactory pageFactory = PageFactory.getInstance();
private Page page = null;
private int actionType ;
public StandardPageBuilder(PageEvent pageEvent) {
this.page = pageEvent.getPage();
this.actionType = pageEvent.getActionType();
}
public Menu buildMenu(Integer Id) {
switch(actionType){
case PageEvent.CREATE:
return createMenu(Id);
case PageEvent.QUERY:
return queryMenu(Id);
case PageEvent.EDIT:
return updateMenu(Id);
case PageEvent.DELETE:
return deleteMenu(Id);
default:
return null;
}
}
/**
* write the content to a html file
* @param pageEvent
* @return String content file name
*/
public String buildContent(Integer Id) {
switch(actionType){
case PageEvent.CREATE:
return writeContent(Id);
case PageEvent.QUERY:
return queryContent(Id);
case PageEvent.EDIT:
return writeContent(Id);
case PageEvent.DELETE:
deleteContent(Id);
break;
}
return null;
}
/**
* create the jsp for the page, such as xx.jsp
*
* @return
*/
public boolean buildTemplate(Integer Id) {
switch(actionType){
case PageEvent.CREATE:
return createTemplate(Id);
case PageEvent.EDIT:
return createTemplate(Id);
case PageEvent.DELETE:
deleteTemplate(Id);
break;
}
return false;
}
/**
* 1. create new Menu Object
* 2. save the object to the persistence
* @param pageEvent
* @return Menu
*/
private Menu createMenu(Integer Id) {
Debug.logVerbose(" -->> enter build menu:" + page.getName(), module);
Menu menu = pageFactory.createMenu(Id);
pageFactory.updateMenu(menu, page);
Debug.logVerbose("menu object created, ID is " + menu.getId(), module);
Navlink navlink = page.getNavlink();
navlink.setId(Id);
//add the menu to the navlink
navlink.addMenu(menu);
try {
//write Obejct to XML file;
pageFactory.saveNavlink(navlink);
Debug.logVerbose(" --> >build Menu ok", module);
page.setNavlink(navlink);
} catch (Exception ex) {
Debug.logError(" build Menu error:" + ex, module);
menu = null;
}
return menu;
}
private Menu updateMenu(Integer Id){
Debug.logVerbose(" -->> enter update menu:" + page.getName(), module);
Navlink navlink = page.getNavlink();
Menu menu = pageFactory.getMenu(Id, navlink);
pageFactory.updateMenu(menu, page);
navlink.setId(Id);
try {
//write Obejct to XML file;
pageFactory.saveNavlink(navlink);
Debug.logVerbose(" --> >update Menu ok", module);
page.setNavlink(navlink);
} catch (Exception ex) {
Debug.logError(" update Menu error:" + ex, module);
menu = null;
}
return menu;
}
private Menu queryMenu(Integer Id) {
Navlink navlink = page.getNavlink();
Menu menu = pageFactory.getMenu(Id, navlink);
if (menu != null)
page.setName(menu.getName());
page.setId(Id);
return menu;
}
private Menu deleteMenu(Integer Id) {
Navlink navlink = page.getNavlink();
Menu menu = pageFactory.getMenu(Id, navlink);
if (menu != null)
pageFactory.deleteMenu(menu, navlink);
try {
//write Obejct to XML file;
pageFactory.saveNavlink(navlink);
Debug.logVerbose(" --> >delete menu ok", module);
page.setNavlink(navlink);
} catch (Exception ex) {
Debug.logError(" delete Menu error:" + ex, module);
menu = null;
}
return null;
}
public String writeContent(Integer Id) {
String output = null;
try {
output = pageFactory.saveContent(Id, page.getHtmlText());
//htmlText was even the content of the page,
//now it is the file name that contains the content of the page
page.setHtmlText(output);
} catch (Exception ex) {
Debug.logError("buildContent error:" + ex, module);
}
return output;
}
public String queryContent(Integer Id) {
String content = pageFactory.getContent(Id);
page.setHtmlText(content);
return content;
}
private void deleteContent(Integer Id){
pageFactory.deleteContent(Id);
}
/**
* 1. get every menu from the navlink
* 2. get body from every menu;
* 3. write every body to file;
* @return
*/
public Body buildBody(Integer Id) {
switch(actionType){
case PageEvent.CREATE:
return createBody(Id);
case PageEvent.QUERY:
return queryBody(Id);
case PageEvent.EDIT:
return updateBody(Id);
case PageEvent.DELETE:
return deleteBody(Id);
default:
return null;
}
}
private Body createBody(Integer Id) {
Debug.logVerbose(" -->> enter build Body " + page.getTitle(), module);
String author = "";
String title = page.getTitle();
//body's content is the file name that contains the content of the page
String content = page.getHtmlText();
Integer hits = new Integer(0);
Integer status = new Integer(0);
Body body = null;
try {
body = pageFactory.createBody(Id);
body.setAuthor(author);
body.setTitle(title);
body.setContent(content);
body.setHits(hits);
body.setStatus(status);
pageFactory.updateBody(body);
} catch (Exception e) {
Debug.logError(" build body error in :" + e);
}
return body;
}
public Body queryBody(Integer Id) {
Body body = pageFactory.getBody(Id);
page.setTitle(body.getTitle());
return body;
}
private Body updateBody(Integer Id) {
Debug.logVerbose(" -->> enter update Body " + page.getTitle(), module);
String title = page.getTitle();
//body's content is the file name that contains the content of the page
String content = page.getHtmlText();
Body body = null;
try {
body = pageFactory.getBody(Id);
body.setTitle(title);
body.setContent(content);
pageFactory.updateBody(body);
} catch (Exception e) {
Debug.logError(" update body error in :" + e);
}
return body;
}
public Body deleteBody(Integer Id) {
Body body = pageFactory.getBody(Id);
if (body != null)
pageFactory.deleteBody(body);
return body;
}
public boolean createTemplate(Integer Id) {
boolean success = false;
Menu menu = null;
try {
Debug.logVerbose("--> find the menu by the Id: " + Id, module);
menu = pageFactory.getMenu(Id, page.getNavlink());
if (menu == null)
return success;
String titleValue = menu.getName();
String bodyValue = menu.getDataLink();
Debug.logVerbose("--> jspFileName =" + menu.getLink() + " bodyValue=" +
bodyValue, module);
String jspPathName = XmlUtil.getJspDirStr(menu.getId().toString());
success = JspMaker.generate(jspPathName, titleValue, bodyValue);
} catch (Exception ex) {
Debug.logError(" nof get the menu id =" + Id, module);
return success;
}
return success;
}
/**
*
* @param Id
*/
private void deleteTemplate(Integer Id){
Menu menu = null;
try {
menu = pageFactory.getMenu(Id, page.getNavlink());
if (menu == null) return ;
String jspPathName = XmlUtil.getJspDirStr(menu.getId().toString());
JspMaker.delete(jspPathName);
} catch (Exception ex) {
Debug.logError(" not get the menu id =" + Id, module);
}
}
public Page getPage() {
return this.page;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -