AnsweredAssumed Answered

Issue on Programmatic Versioning of manually uploaded file

Question asked by junieboy on Nov 6, 2010
Latest reply on Nov 17, 2010 by junieboy
Hi,

Here's the scenario: A file exists in the folder. This file was manually uploaded. Now using the Authoring Service API via web service I check out the file, update and check-in. The version increments to 1.0 and modified by are correctly updated but the previous version does not show up in the properties. Any ideas how to solve this?

Below is my code on how to do versioning:


    public UploadResponse updateDocument(Reference ref, String strContent, UploadRequest req)
            throws AuthoringFault, RemoteException {
        log.info("start call to VersionUtilImpl.updateDocument");
        UploadResponse res = new UploadResponse();
        ICleanupUtil axisStubCleanupUtil = new AxisStubCleanupUtil();

        RepositoryServiceSoapBindingStub repositoryService = null;
        AuthoringServiceSoapBindingStub authoringService = null;

        try {

            repositoryService = WebServiceFactory.getRepositoryService();

            /*
             * Use authoring service to checkout file, update content, then
             * check back in with major revision
             */
            authoringService = WebServiceFactory.getAuthoringService();

            /*
             * Checkout the document to update, placing the working document in
             * the same folder
             */
            Predicate itemToCheckOut = new Predicate(new Reference[] { ref }, null, null);

            /*
             * First confirm that the content reference does not have the
             * versionable aspect applied
             */
            Node node = repositoryService.get(itemToCheckOut)[0];
            for (String aspect : node.getAspects()) {
                log.info("aspect: " + aspect);
                if (Constants.ASPECT_VERSIONABLE.equals(aspect) == false) {
                    /*
                     * Add the versionable aspect to the newly created content.
                     * This will allows the content to be versioned
                     */
                    makeVersionable(itemToCheckOut);
                }
            }

            CheckoutResult checkOutResult = authoringService.checkout(itemToCheckOut, null);

            /* Get a reference to the working copy */
            Reference workingCopyReference = checkOutResult.getWorkingCopies()[0];

            IMetadataUpdates metadataUpdates = new MetadataUpdatesImpl();
            NamedValue[] properties = metadataUpdates.organizeUpdates(req, strContent);

            Predicate where = new Predicate(new Reference[] { workingCopyReference }, null, null);

            CML cml = new CML();
            CMLUpdate update = new CMLUpdate(properties, where, null);
            cml.setUpdate(new CMLUpdate[] { update });

            try {
                repositoryService.update(cml);
            } catch (RepositoryFault e) {
                log.error("RepositoryFault!", e);
                throw e;
            } catch (RemoteException e) {
                log.error("RemoteException!", e);
                throw e;
            }

            /*
             * Now check the working copy in with a description of the change
             * made that will be recorded in the version history
             */
            String versionDescription = "This is a default description. The content has been updated.";
            String desc = req.getDmsRqstFld2();
            if (desc != null && !desc.equalsIgnoreCase("")) {
                versionDescription = desc;
            }

            log.info("Notes for this version got from dmsRqstFld2: " + versionDescription);
            NamedValue[] revisionType = new NamedValue[] { Utils.createNamedValue("versionType", "MAJOR"),
                    Utils.createNamedValue("description", versionDescription) };
            CheckinResult checkinResult = authoringService.checkin(where, revisionType, false);

            Reference[] refs = checkinResult.getCheckedIn();

            /*
             * Make sure to get the uuid of the latest file version in the
             * CheckinResult object
             */
            String xpath = refs[0].getPath();
            log.info("getPath(): " + xpath);
            String strUuid = refs[0].getUuid();
            log.info("getUuid(): " + strUuid);
            String strDestStoreAddress = refs[0].getStore().getAddress();
            log.info("getAddress(): " + strDestStoreAddress);

            VersionHistory versionHistory = authoringService.getVersionHistory(refs[0]);
            Version[] versions = versionHistory.getVersions();
            Version version = versions[0];

            String versionLabel = version.getLabel();
            log.info("versionLabel: " + versionLabel);

            String strAfrescoUrlPrefix = PropertyUtils.getInstance().getProps().getProperty(
                    "alfresco.url.prefix");
            String alfrescoUrl = strAfrescoUrlPrefix + strDestStoreAddress + "/" + strUuid + "/"
                    + req.getFileName();
            log.info("Alfresco URL: " + alfrescoUrl);
            String strCfoPortalUrlPrefix = PropertyUtils.getInstance().getProps().getProperty(
                    "cfoportal.url.prefix");
            String dmsUrl = strCfoPortalUrlPrefix + strUuid + "/" + req.getFileName();
            log.info("CFO Portal URL: " + dmsUrl);
            res.setDmsFileUrl(dmsUrl);
            res.setStatus("0");
            res.setMessage("Successfully updated file and incremented version in DMS!");

            res.setVersion(versionLabel);
            res.setXpath(xpath);
            res.setUuid(strUuid);
            res.setStoreAddress(strDestStoreAddress);

        } catch (RepositoryFault e) {
            log.error("RepositoryFault!", e);
            throw e;
        } catch (RemoteException e) {
            log.error("RemoteException!", e);
            throw e;
        } finally {
            axisStubCleanupUtil.cleanup(repositoryService);
            axisStubCleanupUtil.cleanup(authoringService);
        }

        log.info("done call to VersionUtilImpl.updateDocument");
        return res;
    }

    public void makeVersionable(Predicate itemToCheckOut) throws RepositoryFault, RemoteException {

        RepositoryServiceSoapBindingStub repositoryService = null;
        ICleanupUtil cleanupUtil = new AxisStubCleanupUtil();

        try {
            /* Create the add aspect query object */
            CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_VERSIONABLE, null, itemToCheckOut,
                    null);

            /* Create the content management language query */
            CML cml = new CML();
            cml.setAddAspect(new CMLAddAspect[] { addAspect });

            repositoryService = WebServiceFactory.getRepositoryService();
            /*
             * Execute the query, which will add the versionable aspect to the
             * node in question
             */
            repositoryService.update(cml);

        } catch (RepositoryFault e) {
            cleanupUtil.cleanup(repositoryService);
            throw e;
        } catch (RemoteException e) {
            cleanupUtil.cleanup(repositoryService);
            throw e;
        }
    }

    public String[] getVersionDetails(Version version) {
        String[] versionLabelAndDesc = new String[2];

        String versionLabel = null;
        String description = null;
        for (NamedValue namedValue : version.getCommentaries()) {
            if (namedValue.getName().equals("description") == true) {
                description = namedValue.getValue();
            }
        }

        versionLabel = version.getLabel();
        versionLabelAndDesc[0] = versionLabel;
        versionLabelAndDesc[1] = description;
        return versionLabelAndDesc;
    }

Outcomes