发表新闻的逻辑门面(createnewsitemaction.java)
public actionforward execute(actionmapping mapping, actionform form,
httpservletrequest request, httpservletresponse response) throws exception {
//如果点击”cancel”,返回发表新闻页面
if ( iscancelled(request) ) {
return mapping.findforward("callcreateitem");
}
if( request.getsession().getattribute(newsmodulewebconstants.news_item_
submitted_action) == null ) {
request.getsession().setattribute(newsmodulewebconstants.
news_item_submitted_action, "create");
newsitemform newsitemform = (newsitemform) form;
//申明newsitem的对象,并给该对象赋值
newsitem newsitem = new newsitem();
//利用beanutils.copyproperties直接拷贝属性构造newsitem对象
webapputil.copyproperties(newsitem, newsitemform, request);
//newsitem和newsitemform对象间存在名称不相同的属性,
//则beanutils不对这些属性进行处理,需要程序员手动处理
newsitem.setactive(boolean.true);
newsitem.settitle(newsitemform.gettitlemap());
newsitem.setannotation(newsitemform.getannotationmap());
newsitem.setbody(newsitemform.getbodymap());
//获得daomanager对象,类型在spring配置,指向类newsmanagerimpl
newsmanager newsmanager =
(newsmanager) getbean(newsmoduleconstants.
news_manager_bean);(1)
long newsitemid = null;
string uri = null;
try {
//通过newsitemmanager建立新闻实体
newsitemid = newsmanager.createnewsitem(newsitem);
uri = newsmodulewebconstants.news_item_uri_prefix + "/" +
newsitemid;
newsitem.seturi(uri);
newsmanager.updatenewsitem(newsitem);
if ( newsitem.getactive().booleanvalue() ) {
// index news item
searchmanager searchmanager =
searchmanager.getinstance(request.getsession().
getservletcontext());
searchmanager.reindexpage(newsitem, request);
}
} catch ( beanalreadyexistsexception e ) {
… …
return mapping.getinputforward();
}
}
return mapping.findforward("listnewsitems");
}
}
(1)dao管理类newsmanagerimpl类中的处理
public long createnewsitem(newsitem newsitem) throws beanalreadyexistsexception {
//在manager类中,应用将具体操作委派个具体的dao类
if ( pagedao.hasduplicates(newsitem) ) { (2)
… …
throw new beanalreadyexistsexception(errormessage);
// linkable item does not exist
long newsitemid = newsdao.createnewsitem(newsitem);(4)
return newsitemid;
}
(2)解释, pagedaohibernate类中判断新闻的url信息是否有重复
public boolean hasduplicates(page page) {
arraylist args = new arraylist();
args.add(page.geturi());
string hql = "select count(p.id) from page p where p.uri = ?";
if ( page.getid() != null ) {
hql += " and p.id != ?";
args.add(page.getid());
}
int count = ((integer) finduniqueresult(hql, args.toarray())).intvalue();(3)
return count > 0;
}
(3)的解释,basedaohibernate类
public object finduniqueresult(final string hql, final object[] args, final type[]
types, final boolean cacheable, final string cacheregion) {
//使用内隐类
return gethibernatetemplate().execute(new hibernatecallback() {
public object doinhibernate(session session) throws hibernateexception {
query query = session.createquery(hql);
query.setcacheable(cacheable);
if ( cacheregion != null ) {
query.setcacheregion(cacheregion);
}
//查询参数赋值
if ( args != null ) {
for ( int i = 0; i < args.length; i++ ) {
object arg = args[i];
type type = null;
if ( types != null && i < types.length ) {
type = types[i];
}
if ( type == null ) {
query.setparameter(i, arg);
} else {
query.setparameter(i, arg, type);
}
}
}
return query.uniqueresult();
}
});
(4)的解释,newsdaohibernate类,这里是写入数据库的主要逻辑
public long createnewsitem(newsitem newsitem) {
//写入news_item表
newsitem.setusagecounter(new integer(0));
long newsitemid = (long) gethibernatetemplate().save(newsitem);
//写入field表
// title
contentfield ftitle = new contentfield();
ftitle.setidentifier("title");
ftitle.settype(contentfield.line_type);
ftitle.setinternal(boolean.true);
newsitem.addcontentfield(ftitle);
gethibernatetemplate().save(ftitle);
// annotation
contentfield fannotation = new contentfield();
fannotation.setidentifier("annotation");
fannotation.settype(contentfield.html_type);
fannotation.setinternal(boolean.true);
newsitem.addcontentfield(fannotation);
gethibernatetemplate().save(fannotation);
// body
contentfield fbody = new contentfield();
fbody.setidentifier("body");
fbody.settype(contentfield.html_type);
fbody.setinternal(boolean.true);
newsitem.addcontentfield(fbody);
gethibernatetemplate().save(fbody);
//写入field_value表
list contentlocales = executefind("from contentlocale l", true, null);
for ( int i = 0; i < contentlocales.size(); i++ ) {
contentlocale contentlocale = (contentlocale) contentlocales.get(i);
string localeidentifier = contentlocale.getidentifier();
string valuetitle = (string) newsitem.gettitle().get(localeidentifier);
string valueannotation = (string)
newsitem.getannotation().get(localeidentifier);
string valuebody = (string) newsitem.getbody().get(localeidentifier);
if ( (valuetitle == null || valuetitle.trim().length() == 0) && (valueannotation == null || valueannotation.trim().length() == 0) && (valuebody == null || valuebody.trim().length() == 0) ) {
continue;
}
//title
contentfieldvalue fvtitle = new contentfieldvalue();
fvtitle.setcontentlocale(contentlocale);
if ( valuetitle != null ) {
fvtitle.setsimplevalue(valuetitle);
} else {
fvtitle.setsimplevalue("");
}
fvtitle.setlastupdateddatetime(new date());
ftitle.addcontentfieldvalue(fvtitle);
gethibernatetemplate().save(fvtitle);
//annotation
contentfieldvalue fvannotation = new contentfieldvalue();
fvannotation.setcontentlocale(contentlocale);
if ( valueannotation != null ) {
fvannotation.setvalue(convertutil.converttobytearray(valueannotation));
} else {
fvannotation.setvalue(new byte[0]);
闽公网安备 35060202000074号