/**
 * Copyright (c) 2015-present Jorge Díaz All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package jorgediazest.indexchecker.model;

import com.liferay.portal.kernel.dao.orm.Criterion;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ProjectionList;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portlet.documentlibrary.model.DLFileVersion;

import java.util.List;
import java.util.Map;

import jorgediazest.indexchecker.data.Data;

import jorgediazest.util.model.Model;

/**
 * @author Jorge Díaz
 */
public class DLFileEntry extends IndexCheckerModel {

	public Map<Long, Data> getLiferayData(Criterion filter) throws Exception {
		Map<Long, Data> dataMap = super.getLiferayData(filter);

		Model modelDLFileVersion = this.getModelFactory().getModelObject(
			null, DLFileVersion.class.getName());

		DynamicQuery queryDLFileVersion =
			modelDLFileVersion.getService().newDynamicQuery();

		ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
		projectionList.add(
			modelDLFileVersion.getPropertyProjection("fileEntryId"));
		projectionList.add(modelDLFileVersion.getPropertyProjection("status"));

		queryDLFileVersion.setProjection(projectionList);

		queryDLFileVersion.add(filter);

		@SuppressWarnings("unchecked")
		List<Object[]> results =
			(List<Object[]>)modelDLFileVersion.getService().executeDynamicQuery(
				queryDLFileVersion);

		for (Object[] result : results) {
			long fileEntryId = (Long)result[0];
			int status = (Integer)result[1];

			if ((status == WorkflowConstants.STATUS_APPROVED) ||
				(status == WorkflowConstants.STATUS_IN_TRASH)) {

				dataMap.get(fileEntryId).setStatus(status);
			}
			else {
				dataMap.remove(fileEntryId);
			}
		}

		return dataMap;
	}

}
@@ -1023,16 +1023,44 @@
 til.Map;
+%0Aimport java.util.Map.Entry;
 %0A%0Aimport
@@ -1096,16 +1096,64 @@
 ta.Data;
+%0Aimport jorgediazest.indexchecker.data.DataUtil;
 %0A%0Aimport
@@ -1835,16 +1835,91 @@
 atus%22));
+%0A%09%09projectionList.add(modelDLFileVersion.getPropertyProjection(%22version%22));
 %0A%0A%09%09quer
@@ -2268,16 +2268,201 @@
 sult%5B1%5D;
+%0A%09%09%09String version = (String)result%5B2%5D;%0A%0A%09%09%09Data data = dataMap.get(fileEntryId);%0A%0A%09%09%09if ((data == null) %7C%7C%0A%09%09%09%09!DataUtil.exactStrings(version, data.getVersion())) %7B%0A%0A%09%09%09%09continue;%0A%09%09%09%7D
 %0A%0A%09%09%09if 
@@ -2573,36 +2573,16 @@
 %09%09%09%09data
-Map.get(fileEntryId)
 .setStat
@@ -2604,13 +2604,110 @@
 %7D%0A%09%09
-%09else
+%7D%0A%0A%09%09for (Entry%3CLong, Data%3E entry : dataMap.entrySet()) %7B%0A%09%09%09if (entry.getValue().getStatus() == null)
  %7B%0A%09
@@ -2724,27 +2724,30 @@
 .remove(
-fileEntryId
+entry.getKey()
 );%0A%09%09%09%7D%0A

Improve DLFileVersion.status handling