[opencms-dev] how to creat a custom collector

Daniel Wiesner daniel.wiesner at av-studio.de
Fri Jun 3 18:37:20 CEST 2005


hello mickael,

MickyW schrieb:

>hi,
>I'd like to create a new ressource collector but I can't find any
>information on how to do this.
>To be more specific, I need to know :
>- which class to subclass (or an example of collector class so I could just
>study existing code)
>  
>
subclass org.opencms.file.collectors.A_CmsResourceCollector.

e.g: 
package collectors;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.A_CmsResourceCollector;
import org.opencms.file.collectors.CmsCollectorData;
import org.opencms.main.CmsException;

public class IgnoreExpirationCollector extends A_CmsResourceCollector {

	
	private static final String[] m_collectorNames = {
		"singleFileIgnoreExpiration",
        "allInFolderIgnoreExpirationDateReleasedDesc"
       
    };
	
	
	  /** Array list for fast collector name lookup. */
    private static final List m_collectors = Collections.unmodifiableList(Arrays.asList(m_collectorNames));

	
	public List getCollectorNames() {
		return m_collectors;
	}

	
	
	 /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateLink(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
     */
	public String getCreateLink(CmsObject cms, String collectorName,
			String param) throws CmsException {
		 // if action is not set, use default action
        if (collectorName == null) {
            collectorName = m_collectorNames[0];
        }

        switch (m_collectors.indexOf(collectorName)) {
            case 0:
                // "singleFileIgnoreExpiration"
                
            case 1:
                // "allInFolderIgnoreExpirationDateReleasedDesc"
				  return getCreateInFolder(cms, param);    
            default:
                throw new CmsException("Invalid resource collector selected: " + collectorName);
        }
	}

	public String getCreateParam(CmsObject cms, String collectorName,
			String param) throws CmsException {
		
		//	 if action is not set, use default action
        if (collectorName == null) {
            collectorName = m_collectorNames[0];
        }

        switch (m_collectors.indexOf(collectorName)) {
            case 0:
                // "singleFileIgnoreExpiration"
                return null;
            case 1:
                // "allInFolderIgnoreExpirationDateReleasedDesc"
               return param;                
           
            default:
                throw new CmsException("Invalid resource collector selected: " + collectorName);
        }
	}

	public List getResults(CmsObject cms, String collectorName, String param)
			throws CmsException {
		
				  // if action is not set use default
		        if (collectorName == null) {
		            collectorName = m_collectorNames[0];
		        }

		        switch (m_collectors.indexOf(collectorName)) {
		            case 0:
		                // "singleFileIgnoreExpiration"
						return getSingleFileIgnoreExpiration(cms, param);
		            case 1:
		                // "allInFolderIgnoreExpirationDateReleasedDesc"
		                return getAllInFolderIgnoreExpirationDateReleasedDesc(cms, param, false);
		          
		            default:
		                throw new CmsException("Invalid resource collector selected: " + collectorName);
		        }

	}






	/**
     * Returns a List of all resources in the folder pointed to by the parameter 
     * sorted by the release date, descending.<p>
     * 
     * @param cms the current CmsObject
     * @param param the folder name to use
     * @param tree if true, look in folder and all child folders, if false, look only in given folder
     * 
     * @return a List of all resources in the folder pointed to by the parameter 
     *      sorted by the release date, descending
     * 
     * @throws CmsException if something goes wrong
     */
    protected List getAllInFolderIgnoreExpirationDateReleasedDesc(CmsObject cms, String param, boolean tree) throws CmsException {

        CmsCollectorData data = new CmsCollectorData(param);
        String foldername = CmsResource.getFolderPath(data.getFileName());

        CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(data.getType());
        List result = cms.readResources(foldername, filter, tree);

        Collections.sort(result, CmsResource.COMPARE_DATE_RELEASED);

        return shrinkToFit(result, data.getCount());
    }



	/**
     * Returns a List containing the resources pointed to by the parameter.<p>
     * 
     * @param cms the current CmsObject
     * @param param the name of the file to load
     * 
     * @return a List containing the resources pointed to by the parameter
     * 
     * @throws CmsException if something goes wrong
     */
    protected List getSingleFileIgnoreExpiration(CmsObject cms, String param) throws CmsException {

        if ((param == null) || (cms == null)) {
            throw new IllegalArgumentException("Collector requires a parameter in the form '/myfolder/file.html'");
        }

        // create a list and return it
        ArrayList result = new ArrayList(1);
        result.add(cms.readFile(param,CmsResourceFilter.IGNORE_EXPIRATION));
        return result;
    }

}



>- how to make my collector available in OpenCms like the other ones (through
>the property.collector of a resource)
>  
>
register your class in /WEB-IND/conf/opencms-vfs.xml. Look for this node:
        <collectors>
                <collector 
class="org.opencms.file.collectors.CmsPriorityResourceCollector" 
order="100" />
                <collector 
class="org.opencms.file.collectors.CmsDefaultResourceCollector" 
order="110" />
            </collectors>        

>- if it is possible to put such a class in a module and to make it available
>y just installing the module (without further file config)
>  
>
I think you have to edit the config.

>- where to modify the format of the param of the collector: current format
>is  "[filename]|[resource type]|[count]"as I would like to add collector
>specific optionnal param(or perhaps I can do this without modifying OpenCms
>code ?)
>  
>
this depends on your collector implementation .

>what I want to do is something similar to sql queries : i want X first
>essources from folder A whose property P contains a certain tag T where X,
>P, T would be custom optional parameters.Would take a few seconds with a
>database query and seems to be a pretty asic need for a CMS
>but I don't see any other way to do this with OpenCms. Perhaps am I missing
>something?
>thanks in advance
>  
>
I think this is possible with a collector. You have to parse the params  
and do some cms logic in getResults() of your collector class.

>mickael
>
>
>
>
>
>
>_______________________________________________
>This mail is send to you from the opencms-dev mailing list
>To change your list options, or to unsubscribe from the list, please visit
>http://mail.opencms.org/mailman/listinfo/opencms-dev
>  
>

Daniel

-- 
-----------------------------------------------------------
AV-Studio Kommunikationsmedien GmbH Halle            
Kurallee 13, D-06114 Halle (Saale)

Fon: +49 (0345) 20 999 -0 * Fax: +49 (0345) 20 999 -20
email: office at av-studio.de * http://www.av-studio.de
-----------------------------------------------------------




More information about the opencms-dev mailing list