📄 newsbean.java
字号:
/*
*/
package news;
//import java.rmi.RemoteException;
import javax.ejb.*;
import java.util.*;
import javax.naming.*;
import article.*;
import util.*;
public class NewsBean implements SessionBean {
private LocalSortHome sortHome = null;
private LocalArticleHome articleHome = null;
private LocalAdminHome adminHome = null;
///////////////////////////////////////////// business methods
public int getSortCount(){
Collection c=null;
try{
c=sortHome.findAll();
}
catch(Exception ex){
Debug.print("getSortCount() Error\n\n");
}
int j=0;
Iterator i = c.iterator();
while (i.hasNext()) {
i.next();
j++;
}
return j;
}
public int getMaxSortId(){
Collection sorts=null;
LocalSort sort=null;
int j=0;
try{
sorts=sortHome.findAll();
Iterator i=sorts.iterator();
while(i.hasNext()){
sort=(LocalSort)i.next();
}
j=sort.getSortId().intValue();
}
catch(Exception ex){
Debug.print("getMaxSortId() Error\n\n");
}
return j;
}
public int getMaxArticleId(){
Collection articles=null;
LocalArticle article=null;
int j=0;
try{
articles=articleHome.findAll();
Iterator i=articles.iterator();
while(i.hasNext()){
article=(LocalArticle)i.next();
}
j=article.getArticleId().intValue();
}
catch(Exception ex){
Debug.print("getMaxArticleId() Error\n\n");
}
return j;
}
public int getArticleCount(){
Collection c=null;
try{
c=articleHome.findAll();
}
catch(Exception ex){
Debug.print("getArticleCount() Error\n\n");
}
int j=0;
Iterator i = c.iterator();
while (i.hasNext()) {
i.next();
j++;
}
return j;
}
synchronized public boolean createAdmin(String id,String password) {
boolean b=false;
Debug.print("NewsBean createAdmin");
try {
LocalAdmin admin = adminHome.create(id,password);
b=true;
} catch (Exception ex) {
System.out.println("NewsBean-->createAdmin() ERROR!!!");
b=false;
//throw new EJBException(ex.getMessage());
}
return b;
}
synchronized public void createSort(String name) {
Debug.print("NewsBean createSort");
int j=getMaxSortId();
try {
LocalSort sort = sortHome.create(new Integer(j+1),name);
} catch (Exception ex) {
System.out.println("NewsBean-->createSort() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}///111111111/////////////createSort
synchronized public void dropArticleInSort(int articleId, int sortId) { /*此函数被updateArticle()调用,可能由于数据库的完整性,,外部调用updateArticle()*/
Integer sortId2=new Integer(sortId);
Integer articleId2=new Integer(articleId);
Debug.print("NewsBean createArticleInSort");
try {
LocalSort sort = sortHome.findByPrimaryKey(sortId2);
LocalArticle article = articleHome.findByPrimaryKey(articleId2);
sort.dropArticle(article);
} catch (Exception ex) {
System.out.println("NewsBean-->dropArticleInSort() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}////22222222222////////////////////////////////
synchronized public void createArticle(ArticleDetails details){
Debug.print("NewsBean createArticle");
int j=getMaxArticleId();
try{
LocalArticle article = articleHome.create(new Integer(j+1),details.getTitle(),details.getContent(), details.getAddTime());
}
catch(Exception ex){
System.out.println("NewsBean-->createArticle() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}
public void createArticleInSort(int articleId, int sortId) {
Debug.print("NewsBean createArticleInSort");
try {
LocalSort sort = sortHome.findByPrimaryKey(new Integer(sortId));
LocalArticle article = articleHome.findByPrimaryKey(new Integer(articleId));
//try{
// sort.addArticle(article);
//}
//catch(Exception ee){System.out.println("NewsBean-->createArticleInSort-->sort.addArticle(article); ERROR!!!");}
Collection articles=sort.getArticles();
articles.add(article);
} catch (Exception ex) {
System.out.println("NewsBean-->createArticleInSort() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}/////////////////////33333333333333
synchronized public void removeArticle(int articleId) {
Debug.print("NewsBean removeArticle");
try {
LocalArticle article = articleHome.findByPrimaryKey(new Integer(articleId));
article.remove();
} catch (Exception ex) {
System.out.println("NewsBean-->removeArticle() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}//////////////4
synchronized public void removeSort(int sortId) {
Debug.print("NewsBean removeSort");
try {
LocalSort sort = sortHome.findByPrimaryKey(new Integer(sortId));
sort.remove();
} catch (Exception ex) {
System.out.println("NewsBean-->removeSort() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}/////////////////5555555555
synchronized public void removeAdmin(String id) {
Debug.print("NewsBean removeAdmin");
try {
LocalAdmin admin = adminHome.findByPrimaryKey(id);
admin.remove();
} catch (Exception ex) {
System.out.println("NewsBean-->removeAdmin() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}/////////////////5555555555
synchronized public void updateAdmin(String id,String password) {
Debug.print("NewsBean updateAdmin");
try {
LocalAdmin admin = adminHome.findByPrimaryKey(id);
admin.setPassword(password);
} catch (Exception ex) {
System.out.println("NewsBean-->updateAdmin() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}///////////////////66
synchronized public void updateSort(int sortId,String sortName) {
Debug.print("NewsBean updateSort");
try {
LocalSort sort = sortHome.findByPrimaryKey(new Integer(sortId));
sort.setSortName(sortName);
} catch (Exception ex) {
System.out.println("NewsBean-->updateSort() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}///////////////////66
synchronized public void updateArticle(int articleId,String title,String content,int sortId) {
Debug.print("NewsBean updateArticle");
try {
LocalArticle article = articleHome.findByPrimaryKey(new Integer(articleId));
LocalSort sort = sortHome.findByPrimaryKey(new Integer(sortId));
article.setArticleTitle(title);
article.setArticleContent(content);
try{
dropArticleInSort(articleId,article.getSort().getSortId().intValue());
}catch(Exception ee){System.out.println("没有所属类……");}
sort.addArticle(article);
} catch (Exception ex) {
System.out.println("NewsBean-->updateArticle() ERROR!!!");
throw new EJBException(ex.getMessage());
}
}/////////////////777777777777777
private ArrayList copyArticlesToDetails(Collection articles) {
ArrayList detailsList = new ArrayList();
Iterator i = articles.iterator();
while (i.hasNext()) {
LocalArticle article = (LocalArticle) i.next();
ArticleDetails details = new ArticleDetails(article.getArticleId().intValue(),
article.getArticleTitle(), article.getArticleContent(), article.getAddTime());
detailsList.add(details);
}
return detailsList;
} // copyArticlesToDetails
//////////////////////
public ArrayList getArticlesOfSort(int sortId){
ArrayList aArticles=new ArrayList();
try{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -