[opencms-dev] Use of CmsPublishManager in OpenCms 8

Christoph Fröhlich cfauto at folge2.de
Wed Sep 19 14:43:41 CEST 2012


Hi Yves

we do it with the following code. Works for us in 8.0.3

Regards
Christoph

--------------------------------------------------------------------------
    /**
     * publishes the resource.
     * 
     * @param cms cms object
     * @param resourcePath pfad zur resource
     * @param stealLockIfNecessary ob das Lock gestohlen werden soll, wenn das nötig ist
     * @return uuid der Ressource
     * @throws Exception
     */
    public static CmsUUID publishResource(CmsObject cms, String resourcePath, boolean stealLockIfNecessary) throws Exception {

        String resPathModified = cms.getRequestContext().removeSiteRoot(resourcePath);
        if (stealLockIfNecessary) {
            CmsUtils.stealLocks(cms, resPathModified);
        }
        return OpenCms.getPublishManager().publishResource(cms, resPathModified);

    }

    /**
     * publishes the resource and includes related resources.
     * 
     * @param cms cms object
     * @param resourcePath pfad zur resource
     * @param stealLockIfNecessary ob das Lock gestohlen werden soll, wenn das nötig ist
     * @return uuid der Ressource
     * @throws Exception
     */
    public static CmsUUID publishResourceIncludingRelated(CmsObject cms, String resourcePath, boolean stealLockIfNecessary)
            throws Exception {

        String resPathModified = cms.getRequestContext().removeSiteRoot(resourcePath);
        CmsResource resource = cms.readResource(resPathModified);
        if (stealLockIfNecessary) {
            CmsUtils.stealLocks(cms, resource);
        }
        List<CmsResource> toPublish = new ArrayList<CmsResource>();
        toPublish.add(resource);
        CmsPublishList pubList = OpenCms.getPublishManager().getPublishList(cms, toPublish, false);
        CmsPublishList relatedList = OpenCms.getPublishManager().getRelatedResourcesToPublish(cms, pubList);
        pubList = OpenCms.getPublishManager().mergePublishLists(cms, pubList, relatedList);
        CmsLogReport report = new CmsLogReport(ModuleConstants.DEFAULT_LOCALE, null);
        return OpenCms.getPublishManager().publishProject(cms, report, pubList);

    }

   public static void stealLocks(CmsObject cmso, String resource) throws CmsException {
        List<String> locked = cmso.getLockedResources(resource, CmsLockFilter.FILTER_INHERITED);
        if (locked != null && locked.size() > 0) {
            for (String lock : locked) {
                if (F3Logger.log.isDebugEnabled()) {
                    F3Logger.log.debug("Stealing lock of " + lock);
                }
                cmso.changeLock(lock);
            }
        } else {
            CmsLock lock = cmso.getLock(resource);
            if (lock == null || lock.isUnlocked()) {
                cmso.lockResource(resource);
                return;
            } else {
                cmso.changeLock(resource);
            }
        }

    }
Am 19.09.2012 um 10:59 schrieb Yves Glodt:

> Hello,
> 
> In OpenCms 8 there is no longer CmsObject.publishResource(). So I tried to use the CmsPublishManager, but it does not work as expected.
> 
> Here is my code:
> 
> CmsObject cms = OpenCms.initCmsObject("Guest");
> cms.loginUser("user", "password");
> CmsProject project = cms.readProject("Offline");
> cms.getRequestContext().setCurrentProject(project);
> cms.getRequestContext().setSiteRoot("/sites/mysite.lu/");
> 
> CmsPublishManager cmsPublish = new CmsPublishManager();
> cmsPublish.initialize(cms);
> cmsPublish.publishResource(cms,"/images/articles/"+no_article);
> 
> What's wrong with it?
> 
> Best regards,
> Yves
> 
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20120919/898f4f22/attachment.htm>


More information about the opencms-dev mailing list