/*
* $RCSfile: KnowledgeBaseEJB.java,v $ $Revision: 1.1.1.1 $ $Date: 2005/04/28 20:23:11 $ - $Author: mking_cv $
*
* The contents of this file are subject to the Open Software License
* Version 2.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.centraview.com/opensource/license.html
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is: CentraView Open Source.
*
* The developer of the Original Code is CentraView. Portions of the
* Original Code created by CentraView are Copyright (c) 2004 CentraView,
* LLC; All Rights Reserved. The terms "CentraView" and the CentraView
* logos are trademarks and service marks of CentraView, LLC.
*/
package com.centraview.support.knowledgebase;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.centraview.administration.authorization.AuthorizationLocal;
import com.centraview.administration.authorization.AuthorizationLocalHome;
import com.centraview.administration.authorization.ModuleFieldRightMatrix;
import com.centraview.common.CVDal;
import com.centraview.common.CVUtility;
/*
* This Ejb is Staleless SessionBean
* Used for Create, Update, Delete KB and Category
*/
public class KnowledgeBaseEJB implements SessionBean
{
protected javax.ejb.SessionContext ctx;
protected Context environment;
private String dataSource = "MySqlDS";
public KnowledgeBaseEJB ()
{
}
/**
* Called by the container to create a session bean instance. Its parameters typically
* contain the information the client uses to customize the bean instance for its use.
* It requires a matching pair in the bean class and its home interface.
*/
public void ejbCreate ()
{
}
/**
* A container invokes this method before it ends the life of the session object. This
* happens as a result of a client's invoking a remove operation, or when a container
* decides to terminate the session object after a timeout. This method is called with
* no transaction context.
*/
public void ejbRemove ()
{
}
/**
* The activate method is called when the instance is activated from its 'passive' state.
* The instance should acquire any resource that it has released earlier in the ejbPassivate()
* method. This method is called with no transaction context.
*/
public void ejbActivate ()
{
}
/**
* The passivate method is called before the instance enters the 'passive' state. The
* instance should release any resources that it can re-acquire later in the ejbActivate()
* method. After the passivate method completes, the instance must be in a state that
* allows the container to use the Java Serialization protocol to externalize and store
* away the instance's state. This method is called with no transaction context.
*/
public void ejbPassivate ()
{
}
/*
* Set the associated session context. The container calls this method after the instance
* creation. The enterprise Bean instance should store the reference to the context
* object in an instance variable. This method is called with no transaction context.
*/
public void setSessionContext (SessionContext ctx)
{
this.ctx = ctx;
}
/**
* In this method the category and it's member are inserted into the DataBase.
*
* @param catinfo CatValueObject
*/
public int insertCategory (int userId, CategoryVO catinfo)
{
int catid=0;
CVDal dl = new CVDal(dataSource);
try
{
if(catinfo.getCreatedBy() == 0)
catinfo.setCreatedBy(userId);
dl.setSql("support.insertcategory");
dl.setString(1,catinfo.getTitle());
dl.setInt(2,catinfo.getParent());
dl.setInt(3,catinfo.getCreatedBy());
dl.setInt(4,catinfo.getOwner());
dl.setString(5,catinfo.getStatus());
String publishToCustomerView = "NO";
if(catinfo.getPublishToCustomerView() != null && catinfo.getPublishToCustomerView().equals("on")){
publishToCustomerView = "YES";
}
dl.setString(6,publishToCustomerView);
dl.executeUpdate();
catid = dl.getAutoGeneratedKey();
dl.clearParameters();
if(catinfo.getPublishToCustomerView() != null && catinfo.getPublishToCustomerView().equals("on")){
this.setParentPublishCategory(catid,dl);
}
}
catch(Exception e)
{
e.printStackTrace();
} finally {
dl.destroy();
dl = null;
}
return catid;
}// end of insertCategory
/**
* In this method returns the Category Value Object for Passing catId as parameter.
*
* @param catId CatID
* @return CategoryVO ValueObject
*/
public CategoryVO getCategory (int userId, int catId)
{
CategoryVO cat = null;
CVDal dl = new CVDal(dataSource);
try {
dl.setSql("support.getcategory");
dl.setInt(1,catId);
Collection col = dl.executeQuery();
if (col != null) {
Iterator iter = col.iterator();
if (iter.hasNext()) {
HashMap hm = (HashMap)iter.next();
cat = new CategoryVO();
cat.setCatid(((Long)hm.get("catid")).intValue());
cat.setTitle((String)hm.get("title"));
cat.setParent(((Long)hm.get("parent")).intValue());
cat.setCreatedBy(((Long)hm.get("createdby")).intValue());
cat.setModifiedBy(((Long)hm.get("modifiedby")).intValue());
cat.setCreatedOn((Timestamp)hm.get("created"));
cat.setModifiedOn((Timestamp)hm.get("modified"));
cat.setOwner(((Long)hm.get("owner")).intValue());
cat.setStatus((String)hm.get("status"));
String publishToCustomerView = (String)hm.get("publishToCustomerView");
if(publishToCustomerView != null && publishToCustomerView.equals("YES")) {
publishToCustomerView = "on";
} else {
publishToCustomerView = "off";
}
cat.setPublishToCustomerView(publishToCustomerView);
cat.fillAuditDetails(this.dataSource);
}
}
}
catch(Exception e) {
e.printStackTrace();
} finally {
dl.destroy ();
dl = null;
}
return cat;
}
public void updateCategory (int userId,CategoryVO catinfo) throws KBException
{
CVDal dl = new CVDal(dataSource);
int categoryID = catinfo.getCatid();
// Condition if the record status is Publish then Only Owner can update it.
// Other user can't update it.
dl.clearParameters();
dl.setSqlQuery("select status,owner from category where catid=?;");
dl.setInt ( 1, categoryID);
Collection col = dl.executeQuery();
dl.clearParameters();
int ownerID = 0;
String status = "";
if (col != null)
{
Iterator iterator = col.iterator();
HashMap hm = null;
while(iterator.hasNext())
{
hm = (HashMap)iterator.next();
ownerID = ((Number)hm.get("owner")).intValue();
status = (String)hm.get("status");
}
}
if (status != null && status.equals("PUBLISH")){
if(ownerID != userId && ownerID != 0){
throw new KBException(KBException.UPDATE_FAILED,"You can't Update the Published Record. Only owner can update it.");
}
}
boolean parentChild=false;
if (parentChild=isParentChild(userId,catinfo.getParent(),categoryID))
{
throw new KBException(KBException.ADD_TO_CHILD,"The Destination Category is a SubCategory of Source Category");
}
if(catinfo.getModifiedBy() == 0)
catinfo.setModifiedBy(userId);
dl.setSql("support.updatecategory");
dl.setString(1,catinfo.getTitle());
dl.setInt(2,catinfo.getParent());
dl.setInt(3,catinfo.getModifiedBy());
dl.setString(4,catinfo.getStatus());
String publishToCustomerView = "NO";
if(catinfo.getPublishToCustomerView() != null && catinfo.getPublishToCustomerView().equals("on")){
publishToCustomerView = "YES";
}
dl.setString(5,publishToCustomerView);
dl.setInt(6,categoryID);
dl.executeUpdate();
dl.clearParameters();
if(catinfo.getPublishToCustomerView() != null && catinfo.getPublishToCustomerView().equals("on")){
this.setParentPublishCategory(categoryID,dl);
}
dl.destroy();
dl = null;
}// end of updateCategory
private void setParentPublishCategory(int categoryId, CVDal cvdl)
{
int catId=0;
cvdl.setSqlQuery("select parent from category where catid=?;");
cvdl.setInt ( 1, categoryId);
Collection col = cvdl.executeQuery();
cvdl.clearParameters();
if (col != null)
{
Iterator iterator = col.iterator();
HashMap hm = null;
while(iterator.hasNext())
{
hm = (HashMap)iterator.next();
catId = ((Number)hm.get("parent")).intValue();
if (catId != 0){
cvdl.setSqlQuery("update category set publishToCustomerView='YES' where catid=?;");
cvdl.setInt ( 1, catId);
cvdl.executeUpdate();
this.setParentPublishCategory(catId,cvdl);
}
}
}
}
private boolean isParentChild(int userID,int newParentID,int categoryID)
{
CVDal dl=new CVDal(dataSource);
Vector vecDDNameValue=new Vector();
boolean parentChild=false;
try {
int catID=newParentID;
if(newParentID==categoryID)
return parentChild=true;
while (catID!=0||parentChild)
{
dl.clearParameters();
dl.setSql( "kb.getparentcat");
dl.setInt(1,catID);
Collection colID=dl.executeQuery();
Iterator iter =colID.iterator();
int parID=0;
while (iter.hasNext())
{
HashMap hm=(HashMap)iter.next();
parID=((Long)hm.get("parent")).intValue();
}
if (parID==categoryID)
{
parentChild=true;
break;
}
else
catID=parID;
}
} finally {
dl.destroy();
dl = null;
}
return parentChild;
}
/*
* This method delete the Category
*
* catId is the Category Id which is to be deleted.
*/
public int deleteCategory (int userId, int categoryId)
{
CVDal cvdl = null;
try
{
cvdl = new CVDal(dataSource);
deleteCategoryTree (userId,categoryId,cvdl);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if (cvdl != null)
{
cvdl.destroy();
}
}
return 0;
}
/*
* This method delete the Category from and all subcategories
*
* catId is the Category Id which is to be deleted.
*/
private void deleteCategoryTree (int userId, int categoryId, CVDal cvdl)
{
int catId=0;
try
{
cvdl.setSql("support.getsubcategory");
cvdl.setInt ( 1, categoryId);
Collection col = cvdl.executeQuery();
cvdl.clearParameters();
if (col != null)
{
Iterator iterator = col.iterator();
HashMap hm = null;
while(iterator.hasNext())
{
hm = (HashMap)iterator.next();
catId = ((Long)hm.get("catid")).intValue();
deleteCategoryTree(userId,catId,cvdl);
}
}
deleteKBForCategory (userId,categoryId);
cvdl.setSql("support.deletecategory");
cvdl.setInt ( 1, categoryId);
cvdl.executeUpdate();
cvdl.clearParameters();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* In this method returns the ArrayList of Category Value Object .
*
* @param userid int
* @return CategoryVO ValueObject
*/
public ArrayList getAllCategory (int userId)
{
ArrayList calegoryList = null;
CVDal dl = new CVDal(dataSource);
try
{
dl.setSql( "support.getallcategory" );
Collection col = dl.executeQuery();
dl.clearParameters();
if (col != null)
{
Iterator iterator = col.iterator();
HashMap hm = null;
calegoryList = new ArrayList();
CategoryVO cat = null;
for(int i=0; iterator.hasNext(); i++)
{
hm = (HashMap)iterator.next();
cat = new CategoryVO();
cat.setCatid(((Long)hm.get("catid")).intValue());
cat.setTitle((String)hm.get("title"));
cat.setParent(((Long)hm.get("parent")).intValue());
cat.setCreatedBy(((Long)hm.get("createdby")).intValue());
cat.setModifiedBy(((Long)hm.get("modifiedby")).intValue());
cat.setCreatedOn((Timestamp)hm.get("created"));
cat.setModifiedOn((Timestamp)hm.get("modified"));
cat.setOwner(((Long)hm.get("owner")).intValue());
cat.fillAuditDetails(this.dataSource);
calegoryList.add(i,cat);
}
}
}
catch(Exception e)
{
e.printStackTrace();
} finally {
dl.destroy ();
dl = null;
}
return calegoryList;
}// end of getCategoryVO
/**
* In this method the Knowledge Base and it's member are inserted into the DataBase.
*
* @param kbinfo KBValueObject
*/
public int insertKB (int userId,KnowledgeVO kbinfo)
{
int kbid=0;
CVDal dl = new CVDal(dataSource);
try
{
if(kbinfo.getCreatedBy() == 0)
kbinfo.setCreatedBy(userId);
if(kbinfo.getOwner() == 0)
kbinfo.setOwner(userId);
dl.setSql("support.insertkb");
dl.setString(1,kbinfo.getTitle());
dl.setString(2,kbinfo.getDetail());
dl.setInt(3,kbinfo.getCreatedBy());
dl.setInt(4,kbinfo.getOwner());
dl.setInt(5,kbinfo.getCatid());
dl.setString(6,kbinfo.getStatus());
String publishToCustomerView = "NO";
if(kbinfo.getPublishToCustomerView() != null && kbinfo.getPublishToCustomerView().equals("on")){
publishToCustomerView = "YES";
}
dl.setString(7,publishToCustomerView);
dl.executeUpdate();
kbid = dl.getAutoGeneratedKey();
InitialContext ic = CVUtility.getInitialContext();
AuthorizationLocalHome authorizationHome = (AuthorizationLocalHome)ic.lookup("local/Authorization");
AuthorizationLocal authorizationLocal = authorizationHome.create();
authorizationLocal.setDataSource(dataSource);
authorizationLocal.saveCurrentDefaultPermission("Knowledgebase", kbid, userId);
}
catch(Exception e)
{
e.printStackTrace();
} finally {
dl.destroy();
dl = null;
}
return kbid;
}// end of insertKB
// Updating KB
public void updateKB (int userId,KnowledgeVO kbinfo) throws KBException
{
if (!CVUtility.canPerformRecordOperation(userId, "Knowledgebase", kbinfo.getKbid(), ModuleFieldRightMatrix.UPDATE_RIGHT, this.dataSource))
throw new KBException(1," You don't have privilege to update this Record. ");
CVDal dl = new CVDal(dataSource);
try
{
dl.setSql("support.updatekb");
dl.setString(1,kbinfo.getTitle());
dl.setString(2,kbinfo.getDetail());
dl.setInt(3,kbinfo.getModifiedBy());
dl.setInt(4,kbinfo.getCatid());
dl.setString(5,kbinfo.getStatus());
String publishToCustomerView = "NO";
if(kbinfo.getPublishToCustomerView() != null && kbinfo.getPublishToCustomerView().equals("on")){
publishToCustomerView = "YES";
}
dl.setString(6,publishToCustomerView);
dl.setInt(7,kbinfo.getKbid());
dl.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
} finally {
dl.destroy ();
dl = null;
}
}// end of updateCategory
/*
* This method delete the Category from Following table
* 1) Member
* 2) Grouptbl
*
* catId is the Category Id which is to be deleted.
*/
// Deleting KB
public void deleteKB (int userId,int kbId)
{
if(CVUtility.canPerformRecordOperation( userId, "Knowledgebase", kbId, 10, this.dataSource)){
CVDal dl = new CVDal(dataSource);
try
{
dl.setSql("support.deletekb");
dl.setInt(1,kbId);
dl.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
} finally {
dl.destroy();
dl = null;
}
}
}
// delete kb for a category
private void deleteKBForCategory (int userId,int categoryId) throws KBException
{
CVDal dl = new CVDal(dataSource);
try
{
dl.setSql("support.deleteallkbforcategory");
dl.setInt(1,categoryId);
dl.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
throw new KBException(KBException.DELETE_FAILED,"Could not delete Knowledgebase for categoryId" +categoryId);
} finally {
dl.destroy();
dl = null;
}
}
// Deleting all KB
public void deleteAllKB (int userId)
{
CVDal dl = new CVDal(dataSource);
try
{
dl.setSql("support.deleteallkb");
dl.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
} finally {
dl.destroy();
dl = null;
}
}
/**
* In this method returns the Category Value Object for Passing catId as parameter.
*
* @param catId CatID
* @return CategoryVO ValueObject
*/
public KnowledgeVO getKB (int userId,int kbId)
{
KnowledgeVO kbv = null;
CVDal dl = new CVDal(dataSource);
try
{
dl.setSql("support.getkb");
dl.setInt(1,kbId);
Collection col = dl.executeQuery();
if (col != null)
{
HashMap hm = (HashMap)col.iterator().next();
kbv = new KnowledgeVO();
kbv.setKbid(((Long)hm.get("kbid")).intValue());
kbv.setTitle((String)hm.get("title"));
kbv.setDetail((String)hm.get("detail"));
kbv.setOwner(((Long)hm.get("owner")).intValue());
kbv.setCreatedBy(((Long)hm.get("createdby")).intValue());
kbv.setModifiedBy(((Long)hm.get("updatedby")).intValue());
kbv.setCatid(((Long)hm.get("category")).intValue());
kbv.setStatus((String)hm.get("status"));
String publishToCustomerView = (String)hm.get("publishToCustomerView");
if(publishToCustomerView != null && publishToCustomerView.equals("YES")){
publishToCustomerView = "on";
}
else{
publishToCustomerView = "off";
}
kbv.setPublishToCustomerView(publishToCustomerView);
}
}
catch(Exception e)
{
e.printStackTrace();
} finally {
dl.destroy();
dl = null;
}
return kbv;
}// end of getKB
public int duplicateCategory(int userId, CategoryVO catVO)
{
int newCatId = -1;
KnowledgeVO kbVO = null;
//CategoryVO catVO = null;
CVDal cvdl = null;
try
{
newCatId = this.insertCategory(userId,catVO);
int catId = catVO.getCatid();
ArrayList kbArr = new ArrayList();
kbArr = getAllKBForCategory (userId,catId);
// get each kbVO ...
for(int i = 0; i < kbArr.size(); i++)
{
kbVO = (KnowledgeVO) kbArr.get(i);
kbVO.setCatid(newCatId);
kbVO.setCreatedBy(userId);
this.insertKB(userId,kbVO);
}
// create the sub category ...
cvdl = new CVDal(dataSource);
cvdl.setSql("support.getsubcategory");
cvdl.setInt ( 1, catId);
Collection col = cvdl.executeQuery();
cvdl.clearParameters();
cvdl.destroy();
cvdl = null;
if (col != null)
{
Iterator iterator = col.iterator();
HashMap hm = null;
while(iterator.hasNext())
{
hm = (HashMap)iterator.next();
catId = ((Long)hm.get("catid")).intValue();
createDuplicateCategoryTree(userId,catId,newCatId);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(cvdl != null)
cvdl.destroy();
cvdl = null;
}
return newCatId;
}
/*
creates a duplicale category with all the sub categories..
*/
private void createDuplicateCategoryTree(int userId, int catId, int parent)
{
int newCatId=-1;
ArrayList kbArr = null;
KnowledgeVO kbVO = null;
CategoryVO catVO = null;
CVDal cvdl = null;
try
{
//create top level category starts..
// get categoryVO
catVO = this.getCategory(userId, catId);
if(parent > 0)
catVO.setParent(parent);
if(catVO.getCreatedBy() == 0)
catVO.setCreatedBy(userId);
// add category..
newCatId = this.insertCategory(userId, catVO);
// write this method....
kbArr = getAllKBForCategory (userId,catId);
// get each kbVO ...
for(int i = 0; i < kbArr.size(); i++)
{
kbVO = (KnowledgeVO) kbArr.get(i);
kbVO.setCatid(newCatId);
kbVO.setCreatedBy(userId);
this.insertKB(userId,kbVO);
}
// create top level category ends..
// create the sub category ...
cvdl = new CVDal(dataSource);
cvdl.setSql("support.getsubcategory");
cvdl.setInt ( 1, catId);
Collection col = cvdl.executeQuery();
cvdl.clearParameters();
cvdl.destroy();
cvdl = null;
if (col != null)
{
Iterator iterator = col.iterator();
HashMap hm = null;
while(iterator.hasNext())
{
hm = (HashMap)iterator.next();
catId = ((Long)hm.get("catid")).intValue();
createDuplicateCategoryTree(userId,catId,newCatId);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(cvdl != null)
cvdl.destroy();
cvdl = null;
}
}
/**
* In this method returns the ArrayList of Category Value Object .
*
* @param userid int
* @return CategoryVO ValueObject
*/
private ArrayList getAllKBForCategory (int userId,int catId)
{
ArrayList kbList = null;
CVDal dl = new CVDal(dataSource);
try
{
dl.setSql( "support.getallkbforcategory" );
dl.setInt(1,catId);
Collection col = dl.executeQuery();
if (col != null)
{
Iterator iterator = col.iterator();
HashMap hm = null;
kbList = new ArrayList();
KnowledgeVO kbv = null;
for(int i=0; iterator.hasNext(); i++)
{
hm = (HashMap)iterator.next();
kbv = new KnowledgeVO();
kbv.setKbid(((Long)hm.get("kbid")).intValue());
kbv.setTitle((String)hm.get("title"));
kbv.setDetail((String)hm.get("detail"));
kbv.setOwner(((Long)hm.get("owner")).intValue());
kbv.setCreatedBy(((Long)hm.get("createdby")).intValue());
kbv.setModifiedBy(((Long)hm.get("updatedby")).intValue());
kbv.fillAuditDetails(this.dataSource);
kbList.add(i,kbv);
}
}
}
catch(Exception e)
{
e.printStackTrace();
} finally {
dl.destroy ();
dl = null;
}
return kbList;
}
/**
* @author Kevin McAllister <kevin@centraview.com>
* This simply sets the target datasource to be used for DB interaction
* @param ds A string that contains the cannonical JNDI name of the datasource
*/
public void setDataSource(String ds) {
this.dataSource = ds;
}
}