Fixing Liferay 6.0.6 Diff Plugin




written by Marco Ferretti on October 15, 2012, at 01:52 PM

Since Liferay 5 there is a very neat diff plugin that works out of the box in the document library. The plugin permits to compare text files but, provided you have activated the Open Office Integration you can compare other kind of files like .doc, .docx and odt. Unfortunately there's a bug in Liferay 6.0.6 that prevents this from working.

The bug

The bug consists in a "wrong" call in the document library portlet's compare document action : the name in the request is actually the document's id and trying to get the extension of the document results in an empty string .

The solution

The solution is quite simple ... provided you can recompile portal-impl.jar :

in com.liferay.portlet.documentlibrary.action.CompareVersionsAction change

protected void compareVersions(RenderRequest renderRequest)

by adding

String title = ParamUtil.getString(renderRequest, "title");

and changing

String extension = FileUtil.getExtension(name)

into

String extension = FileUtil.getExtension(title);

so that the method will look like :

	protected void compareVersions(RenderRequest renderRequest)
		throws Exception {

		ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
			WebKeys.THEME_DISPLAY);

		long companyId = themeDisplay.getCompanyId();
		long userId = themeDisplay.getUserId();

		long fileEntryId = ParamUtil.getLong(renderRequest, "fileEntryId");

		long groupId = themeDisplay.getScopeGroupId();
		long folderId = ParamUtil.getLong(renderRequest, "folderId");
		String name = ParamUtil.getString(renderRequest, "name");
		String title = ParamUtil.getString(renderRequest, "title");
		DLFileEntryPermission.check(
			themeDisplay.getPermissionChecker(), groupId, folderId, name,
			ActionKeys.VIEW);

		String extension = FileUtil.getExtension(title);

		String titleWithExtension = ParamUtil.getString(
			renderRequest, "titleWithExtension");

		String sourceVersion = ParamUtil.getString(
			renderRequest, "sourceVersion");
		String targetVersion = ParamUtil.getString(
			renderRequest, "targetVersion");

		InputStream sourceIs = DLFileEntryLocalServiceUtil.getFileAsStream(
			companyId, userId, groupId, folderId, name, sourceVersion);
		InputStream targetIs = DLFileEntryLocalServiceUtil.getFileAsStream(
			companyId, userId, groupId, folderId, name, targetVersion);

		if (extension.equals("htm") || extension.equals("html") ||
			extension.equals("xml")) {

			String escapedSource = HtmlUtil.escape(StringUtil.read(sourceIs));
			String escapedTarget = HtmlUtil.escape(StringUtil.read(targetIs));

			sourceIs = new UnsyncByteArrayInputStream(
				escapedSource.getBytes(StringPool.UTF8));
			targetIs = new UnsyncByteArrayInputStream(
				escapedTarget.getBytes(StringPool.UTF8));
		}

		if (PrefsPropsUtil.getBoolean(
				PropsKeys.OPENOFFICE_SERVER_ENABLED,
				PropsValues.OPENOFFICE_SERVER_ENABLED) &&
			isConvertBeforeCompare(extension)) {

			String sourceTempFileId = DocumentConversionUtil.getTempFileId(
				fileEntryId, sourceVersion);
			String targetTempFileId = DocumentConversionUtil.getTempFileId(
				fileEntryId, targetVersion);

			sourceIs = DocumentConversionUtil.convert(
				sourceTempFileId, sourceIs, extension, "txt");
			targetIs = DocumentConversionUtil.convert(
				targetTempFileId, targetIs, extension, "txt");
		}

		List<DiffResult>[] diffResults = DiffUtil.diff(
			new InputStreamReader(sourceIs), new InputStreamReader(targetIs));

		renderRequest.setAttribute(
			WebKeys.SOURCE_NAME,
			titleWithExtension + StringPool.SPACE + sourceVersion);
		renderRequest.setAttribute(
			WebKeys.TARGET_NAME,
			titleWithExtension + StringPool.SPACE + targetVersion);
		renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
	}

Alternatively, and if you trust me, you can download a patched Attach:portal-impl.jar from this site. The md5sum f223ae66e1bfb93fb0658e215507e6f5 portal-impl.jar

Hope it helps !

Leave a comment

Name (required)
E-mail (required, will not be published)
Website
Comment

Enter value: Captcha