read-Atleap-7-发表新闻-主逻辑
v 发表新闻的逻辑门面
Ø 通过Manager委托,真正建立新闻的操作在
com.blandware.atleap.persistence.hibernate.news.NewsDAOHibernate类的
createNewsItem()方法
Ø 建立新闻的Struts Action
(com.blandware.atleap.webapp.action.news .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不对这些属性进行处理,需要程序员手动处理
//设置从父类Page中继承的active属性
newsItem.setActive(Boolean.TRUE);
//设置从父类Page中继承的title属性
newsItem.setTitle(newsItemForm.getTitleMap());
newsItem.setAnnotation(newsItemForm.getAnnotationMap());
//设置新闻内容
newsItem.setBody(newsItemForm.getBodyMap());
//获得DAOManager对象,类型在Spring配置,指向类
//com.blandware.atleap.service.news.impl.NewsManagerImpl
NewsManager newsManager =
(NewsManager) getBean(NewsModuleConstants.
NEWS_MANAGER_BEAN);
Long newsItemId = null;
String uri = null;
try {
//通过NewsItemManager建立新闻实体
newsItemId = newsManager.createNewsItem(newsItem); (1)
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)的解释,com.blandware.atleap.persistence.hibernate.news.NewsDAOHibernate类,这里是写入数据库的主要逻辑
public Long createNewsItem(NewsItem newsItem) {
//写入news_item表
//设置从父类Page中继承的属性usageCounter
newsItem.setUsageCounter(new Integer(0));
Long newsItemId = (Long) getHibernateTemplate().save(newsItem);
//写入field表
// 设置title标志位,供其它关联表使用,并不真的写入title
//contentField中记录的是新闻标志位,并不包括真正的内容
ContentField fTitle = new ContentField();
fTitle.setIdentifier("title");
fTitle.setType(ContentField.LINE_TYPE);
fTitle.setInternal(Boolean.TRUE);
//设置从父类Localizable继承的addContentField(fTitle)
//一个newsItem有多个contentField标志
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标志位,供其它关联表使用,并不真的写入title
ContentField fBody = new ContentField();
fBody.setIdentifier("body");
fBody.setType(ContentField.HTML_TYPE);
fBody.setInternal(Boolean.TRUE);
newsItem.addContentField(fBody);
getHibernateTemplate().save(fBody);
// contentLocales中记录的是支持的多国语言标准
List contentLocales = executeFind("from ContentLocale l", true, null);
for ( int i = 0; i < contentLocales.size(); i++ ) {
ContentLocale contentLocale = (ContentLocale) contentLocales.get(i);
//取得语言代码,如en,ru,zh等
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;
}
// ContentFieldValue是真正记录新闻内容的地方
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]);
}
fvAnnotation.setLastUpdatedDatetime(new Date());
fAnnotation.addContentFieldValue(fvAnnotation);
getHibernateTemplate().save(fvAnnotation);
//body
ContentFieldValue fvBody = new ContentFieldValue();
fvBody.setContentLocale(contentLocale);
if ( valueBody != null ) {
//往field_value表中写入新闻内容,field_value是真正存储新闻内容的地
//方,它对应com.blandware.atleap.model.core.ContentFieldValue
fvBody.setValue(ConvertUtil.convertToByteArray(valueBody));
} else {
fvBody.setValue(new byte[0]);
}
fvBody.setLastUpdatedDatetime(new Date());
fBody.addContentFieldValue(fvBody);
getHibernateTemplate().save(fvBody);
}
return newsItemId;
}
注:spring的配置
<bean id="newsManager" parent="txProxyTemplate">
<property name="target">
<bean class="com.blandware.atleap.service.news.impl.NewsManagerImpl">
<property name="newsDAO">
<ref bean="newsDAO" />
</property>
<property name="pageDAO">
<ref bean="pageDAO"/>
</property>
</bean>
</property>
</bean>
<bean id="newsDAO"class="com.blandware.atleap.persistence.hibernate.news
NewsDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- PageDAO: Hibernate implementation -->
<ben id="pageDAO"class="com.blandware.atleap.persistence.hibernate.
core.PageDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
附:com.blandware.atleap.model.news.NewsItem
public class NewsItem extends Page {
protected Map annotation = new HashMap();
protected Map body = new HashMap();
protected Date publicationDate = new Date();
public Date getPublicationDate() {
return publicationDate;
}
public void setPublicationDate(Date publicationDate) {
this.publicationDate = publicationDate;
}
public Map getAnnotation() {
return annotation;
}
public void setAnnotation(Map annotation) {
this.annotation = annotation;
}
public Map getBody() {
return body;
}
public void setBody(Map body) {
this.body = body;
}
}