/*
 * Copyright 2016 EPAM Systems
 * 
 * 
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/commons-dao
 * 
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Report Portal 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */ 

package com.epam.ta.reportportal.database.dao;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.epam.ta.reportportal.database.entity.sharing.AclPermissions;

/**
 * Provide utilities for working with sharable repositories
 * 
 * @author Aliaksei_Makayed
 *
 */
class ShareableRepositoryUtils {

	private ShareableRepositoryUtils() {}

	/**
	 * Create {@link Query} for loading shared to specified project and not owned by specified user
	 * entities.
	 * 
	 * @param projectName
	 * @param owner
	 * @return
	 */
	public static Query createSharedEntityQuery(String owner, String projectName) {
		return Query.query(Criteria.where("acl.entries.projectId").is(projectName))
				.addCriteria(Criteria.where("acl.entries.permissions").is(AclPermissions.READ.name()))
				.addCriteria(Criteria.where("acl.ownerUserId").ne(owner));
	}

	/**
	 * Create {@link Query} for loading entities owned by specified user.
	 * 
	 * @param owner
	 * @param projectName
	 * @return
	 */
	public static Query createOwnedEntityQuery(String owner) {
		return Query.query(Criteria.where("acl.ownerUserId").is(owner));
	}

	/**
	 * Create {@link org.springframework.data.mongodb.core.query.Query} for loading non shared
	 * entities owned by specified user
	 * 
	 * @param owner
	 * @return
	 */
	public static Query createUnsharedEntityQuery(String owner) {
		return Query.query(Criteria.where("acl.ownerUserId").is(owner)).addCriteria(Criteria.where("acl.entries").size(0));
	}

}
@@ -784,17 +784,16 @@
 s/%3E.%0A */
- 
 %0A%0Apackag
@@ -834,16 +834,88 @@
 e.dao;%0A%0A
+import com.epam.ta.reportportal.database.entity.sharing.AclPermissions;%0A
 import o
@@ -1030,81 +1030,8 @@
 y;%0A%0A
-import com.epam.ta.reportportal.database.entity.sharing.AclPermissions;%0A%0A
 /**%0A
@@ -1088,25 +1088,24 @@
 ositories%0A *
- 
 %0A * @author 
@@ -1121,19 +1121,16 @@
 Makayed%0A
- *%0A
  */%0Aclas
@@ -1196,16 +1196,18 @@
 tils() %7B
+%0A%09
 %7D%0A%0A%09/**%0A
@@ -1316,25 +1316,24 @@
 ntities.%0A%09 *
- 
 %0A%09 * @param 
@@ -1339,24 +1339,37 @@
  projectName
+ Project name
 %0A%09 * @param 
@@ -1365,32 +1365,51 @@
 %09 * @param owner
+       Widget owner
 %0A%09 * @return%0A%09 *
@@ -1396,32 +1396,38 @@
 wner%0A%09 * @return
+ Query
 %0A%09 */%0A%09public st
@@ -1669,70 +1669,8 @@
 ()))
-%0A%09%09%09%09.addCriteria(Criteria.where(%22acl.ownerUserId%22).ne(owner))
 ;%0A%09%7D
@@ -1746,25 +1746,24 @@
 ed user.%0A%09 *
- 
 %0A%09 * @param 
@@ -1767,39 +1767,29 @@
 am owner
-%0A%09 * @param projectName
+ Widget owner
 %0A%09 * @re
@@ -1784,32 +1784,38 @@
 wner%0A%09 * @return
+ Query
 %0A%09 */%0A%09public st
@@ -2072,17 +2072,16 @@
 user%0A%09 *
- 
 %0A%09 * @pa
@@ -2085,24 +2085,37 @@
 @param owner
+ Widget owner
 %0A%09 * @return
@@ -2114,16 +2114,22 @@
  @return
+ Query
 %0A%09 */%0A%09p

remove restriction of only not owner shared widgets