⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jahiahomepagesbaseservice.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//////  JahiaHomepagesBaseService////  NK      17.12.2001//package org.jahia.services.homepages;import java.util.Vector;import java.util.Hashtable;import java.util.Enumeration;import java.util.Collections;import java.lang.reflect.*;             				// Constructor, Classimport org.jahia.services.JahiaService;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.JahiaInitializationException;import org.jahia.services.sites.JahiaSite;import org.jahia.services.usermanager.JahiaGroup;import org.jahia.services.usermanager.JahiaUser;import org.jahia.registries.ServicesRegistry;import org.jahia.data.JahiaDOMObject;import org.jahia.utils.JahiaConsole;import org.jahia.services.acl.*;import org.jahia.settings.JahiaPrivateSettings;/** * Homepage services * * @author Khue ng */public class JahiaHomepagesBaseService extends JahiaHomepagesService{		private static final String CLASS_NAME = JahiaHomepagesBaseService.class.getName();	    private static JahiaHomepagesBaseService instance = null;	private JahiaHomepagesPersistance hppServ;	    /** list of cache, one cache for each site */    private Hashtable vCaches = null;	    //--------------------------------------------------------------------------    protected JahiaHomepagesBaseService() throws JahiaException {        JahiaConsole.println( CLASS_NAME,                              " ***** " + CLASS_NAME + " started *****" );        setServiceName (JahiaHomepagesService.class.getName());        vCaches = new Hashtable();    }    //--------------------------------------------------------------------------    public static synchronized JahiaHomepagesBaseService getInstance()     throws JahiaException {        if ( instance == null ){            instance = new JahiaHomepagesBaseService();        }        return instance;    }    //--------------------------------------------------------------------------    /***     * @param JahiaPrivateSettings jSettings     *     */	public void init( JahiaPrivateSettings jSettings )	throws JahiaInitializationException	{		if (!isInitialized ()) {        	try {        		loadHomepagesInCache();	    		mIsServiceInitialized = true;        	} catch (Throwable t ){        		t.printStackTrace();				throw new JahiaInitializationException(CLASS_NAME+".init: Init error");        	}	    }	}    //--------------------------------------------------------------------------	/**	 * return the list of all home page	 *	 * @return Enumeration an enumeration of JahiaHomepage bean	 */    public Enumeration getHomepages() throws JahiaException {				Vector v = new Vector();		Enumeration keys = vCaches.keys();		Hashtable hash = null;		while ( keys.hasMoreElements() ){			hash = (Hashtable) vCaches.get(keys.nextElement());			v.addAll(hash.values());				}				JahiaHomepage hp = createHomepage(	"",											"",								 	   	  	"",								 	   	  	HomepageTypes.HOMEPAGE_LINK,								 	   	  	null,								 	   	  	-1								 	     	);        Collections.sort(v,hp);			return v.elements();	}	    //--------------------------------------------------------------------------	/**	 * return the list of all home pages of a site.	 *	 * @return Enumeration an enumeration of JahiaHomepage bean	 */    public Enumeration getHomepages(JahiaSite site) throws JahiaException {				Vector v = new Vector();		if ( site == null ){			return v.elements();		}		Hashtable hash = (Hashtable)vCaches.get(site.getSiteKey());		if ( hash == null ){			return v.elements();		}		v.addAll(hash.values());				JahiaHomepage hp = createHomepage(	"",											"",								 	   	  	"",								 	   	  	HomepageTypes.HOMEPAGE_LINK,								 	   	  	null,								 	   	  	-1								 	     	);        Collections.sort(v,hp);		return v.elements();				}	    //--------------------------------------------------------------------------	/**	 * return a Homepage bean looking at it id	 *	 * @param int the JahiaHomepage id	 * @return JahiaHomepage the JahiaHomepage bean	 */    public JahiaHomepage getHomepage( int id ) throws JahiaException {		// TODO : access control with ParamBean ?		Vector v = new Vector();		Enumeration keys = vCaches.keys();		Enumeration hpKeys = null;		Hashtable hash = null;		JahiaHomepage hp = null;		while ( keys.hasMoreElements() ){			hash = (Hashtable) vCaches.get(keys.nextElement());			hp = (JahiaHomepage)hash.get(new Integer(id));			if ( hp != null ){				return hp;			}		}		// load from db		hp = hppServ.getInstance().load(id);		if ( hp != null ){			hash = (Hashtable)vCaches.get(hp.getSiteKey());			if ( hash == null ){				// create the cache for this site				hash = new Hashtable();				vCaches.put(hp.getSiteKey(),hash);			}			hash.put(new Integer(id),hp);		}		return hp;	}	    //--------------------------------------------------------------------------	/**	 * Create a new Homepage that is not saved in storage yet.	 *	 * @param String the home page name	 * @param String the home page descr	 * @param String the site key	 * @param int type the home page type	 * @param hashtable the props	 * @param int aclID	 *	 * @return Jahiahomepage the created homepage bean	 */	public JahiaHomepage createHomepage(	String name,											String descr,								 	   	  	String siteKey,								 	   	  	int type,								 	   	  	Hashtable props,								 	   	  	int aclID								 	     	) throws JahiaException {	        JahiaHomepage hp;                try {            // define the types of the parameter of the constructor            Class theParams[] = {Class.forName("java.lang.Integer"),            					 Class.forName("java.lang.String"),            					 Class.forName("java.lang.String"),                                 Class.forName("java.lang.Integer"),                                 Class.forName("java.lang.String"),                                 Class.forName("java.util.Hashtable"),                                 Class.forName("java.lang.Integer")};                                             /*            JahiaConsole.println( CLASS_NAME +".createHomepage ",type+" classe: "                               +HomepageTypes.getInstance().getClassesNames()                               							.get(new Integer(type)) );            */                                           // get the constructor by its name            // the name come from the hashtable                              Constructor thisConstructor =             	Class.forName((String)HomepageTypes.getInstance()            	.getClassesNames().get(new Integer(type)))            	.getDeclaredConstructor(theParams);                                                                 // the parameter values of the constructor            Object args[] = {new Integer(-1), // the id            				 name,            				 descr,            				 new Integer(type),            				 siteKey,                             props,                             new Integer(aclID)};                                                      // call the constructor                            hp = (JahiaHomepage)thisConstructor.newInstance(args);                    } catch(ClassNotFoundException cnfe) {            JahiaConsole.println(CLASS_NAME+".createHomepage: ","(class nf) "+cnfe.toString());			throw new JahiaException (CLASS_NAME+".createHomepage",                "Class not found!",                JahiaException.ERROR, JahiaException.ERROR);		} catch(NoSuchMethodException nsme) {            JahiaConsole.println(CLASS_NAME+".createHomepage: ","(method nf) "+nsme.toString());			throw new JahiaException (CLASS_NAME+".createHomepage",                "Method not found!",                JahiaException.ERROR, JahiaException.ERROR);	    } catch(IllegalAccessException iae) {            JahiaConsole.println(CLASS_NAME+".createHomepage: ","(illegal access) "+iae.toString());			throw new JahiaException (CLASS_NAME+".createHomepage",                "Illegal Access!",                JahiaException.ERROR, JahiaException.ERROR);		} catch(InvocationTargetException ite) {            JahiaConsole.println(CLASS_NAME+".createHomepage: ","(invocation) "+ite.toString());			throw new JahiaException (CLASS_NAME+".createHomepage",                "InvocationTarget exception",                JahiaException.ERROR, JahiaException.ERROR);		} catch(InstantiationException ie) {            JahiaConsole.println(CLASS_NAME+".createHomepage: ","(instantiation) "+ie.toString());			throw new JahiaException (CLASS_NAME+".createHomepage",                "Instantiation exception",                JahiaException.ERROR, JahiaException.ERROR);		}                 // return the new JahiaHomepage        return hp;        	}    //--------------------------------------------------------------------------	/**	 * delete a Homepage from storage and cache	 *	 * @param int the home page id	 */    public void deleteHomepage( int id )    throws JahiaException {				// TODO : access control with ParamBean ?				JahiaHomepage hp = getHomepage(id);		if ( hp == null )			return;		Hashtable hash = null ;		hash = (Hashtable)vCaches.get(hp.getSiteKey());		if ( hash != null ){			hash.remove(new Integer(hp.getID()));		}		hp.delete();	}    //--------------------------------------------------------------------------	/**	 * Save a Homepage. Create it if its id is <=0 and create an ACL if it is <=0.	 * The parent ACL should be the site's acl.	 *	 * @param JahiaHomepage the homepage bean object	 * @param int the parent ACL ID	 */    public void saveHomepage( JahiaHomepage hp, int parentAclID )        throws JahiaException {		// TODO : access control with ParamBean ?				if (hp == null)			return;		         // creates ACL, if needed         if (hp.getAclID() <= 0) {             JahiaBaseACL acl = new JahiaBaseACL ();             if (acl != null) {                 // create a new object by specifying the parent ACL ID                 if (!acl.create (parentAclID)) {                     String message = "Could not create an ACL object for a new homepage.";                     JahiaConsole.println (	CLASS_NAME+".saveHomepage", message );                     throw new JahiaException (	CLASS_NAME+".saveHomepage",                      							message,                             					JahiaException.ACL_ERROR,                              					JahiaException.CRITICAL);                 } else {                     JahiaConsole.println (CLASS_NAME+".saveHomepage",                      			"ACL ["+acl.getID()+"] has just been created!");                 }             } else {                 throw new JahiaException (CLASS_NAME+".saveHomepage",                  				"Could not instanciate the JahiaBaseACL class",                         		JahiaException.ACL_ERROR, JahiaException.CRITICAL);             }             // End Create ACL             hp.setAclID( acl.getID() );         }				hp.save();						Hashtable hash = null ;		hash = (Hashtable)vCaches.get(hp.getSiteKey());		if ( hash == null ){			// create the cache for this site			hash = new Hashtable();			vCaches.put(hp.getSiteKey(),hash);		}		hash.put(new Integer(hp.getID()),hp);	}	//--------------------------------------------------------------------------    /**     * load all homepage definitions from storage in cache     *     */    private void loadHomepagesInCache() throws JahiaException {    	if ( vCaches == null )    		vCaches = new Hashtable();    		    	Vector v = hppServ.getInstance().getHomepages();        if ( v == null )        	return;                JahiaHomepage hp = null;        int size = v.size();        Hashtable hash = null;        for( int i=0 ; i<size ; i++ ){        	hp = (JahiaHomepage)v.get(i);			hash = (Hashtable)vCaches.get(hp.getSiteKey());			if ( hash == null ){				// create the cache for this site				hash = new Hashtable();				vCaches.put(hp.getSiteKey(),hash);			}			hash.put(new Integer(hp.getID()),hp);		}    }    //--------------------------------------------------------------------------    /**     * returns a DOM representation of all home pages of a site     *     * @param String siteKey     */    public JahiaDOMObject getHomepageDefsAsDOM( String siteKey )     throws JahiaException {    	    	return hppServ.getInstance().getHomepageDefsAsDOM(siteKey);	        }    //--------------------------------------------------------------------------    /**     * returns a DOM representation of all home pages props of a site     *     * @param String siteKey     */    public JahiaDOMObject getHomepageDefPropsAsDOM( String siteKey )     throws JahiaException {    	    	return hppServ.getInstance().getHomepageDefPropsAsDOM(siteKey);	        }    //--------------------------------------------------------------------------    /**     * Returns a vector of all home page's Acl ID of this site     * Need this for site extraction     *     * @param String siteKey     */    public Vector getAclIDs( String siteKey  )    throws JahiaException{        return JahiaHomepagesPersistance.getInstance().db_get_all_acl_id(siteKey);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -