当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

read-Atleap-5-主逻辑1-的发表新闻


  发表新闻的逻辑门面(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]);
相关内容
赞助商链接