// Path: src/main/java/net/atos/entng/rbs/BookingUtils.java
// public static List<String> getLocalAdminScope(final UserInfos user) {
// 	Map<String, UserInfos.Function> functions = user.getFunctions();
// 	if (functions != null && functions.containsKey(DefaultFunctions.ADMIN_LOCAL)) {
// 		Function adminLocal = functions.get(DefaultFunctions.ADMIN_LOCAL);
// 		if (adminLocal != null) {
// 			return adminLocal.getScope();
// 		}
// 	}
// 
// 	return new ArrayList<String>();
// }
// 
// Path: src/main/java/net/atos/entng/rbs/BookingStatus.java
// public enum BookingStatus {
// 	CREATED(1), VALIDATED(2), REFUSED(3), SUSPENDED(4);
// 
// 	private final int status;
// 
// 	BookingStatus(int pStatus) {
// 		this.status = pStatus;
// 	}
// 	public int status(){
// 		return status;
// 	}
// }


import org.entcore.common.service.impl.SqlCrudService;
import org.entcore.common.sql.Sql;
import org.entcore.common.sql.SqlStatementsBuilder;
import org.entcore.common.user.UserInfos;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import fr.wseduc.webutils.Either;
import static net.atos.entng.rbs.BookingStatus.CREATED;
import static net.atos.entng.rbs.BookingStatus.REFUSED;
import static net.atos.entng.rbs.BookingStatus.SUSPENDED;
import static net.atos.entng.rbs.BookingUtils.getLocalAdminScope;
import static org.entcore.common.sql.Sql.parseId;
import static org.entcore.common.sql.SqlResult.*;
import static org.entcore.common.sql.SqlResult.validUniqueResultHandler;
import java.util.List;
import net.atos.entng.rbs.BookingStatus;
/*
 * Copyright © Région Nord Pas de Calais-Picardie,  Département 91, Région Aquitaine-Limousin-Poitou-Charentes, 2016.
 *
 * This file is part of OPEN ENT NG. OPEN ENT NG is a versatile ENT Project based on the JVM and ENT Core Project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation (version 3 of the License).
 *
 * For the sake of explanation, any module that communicate over native
 * Web protocols, such as HTTP, with OPEN ENT NG is outside the scope of this
 * license and could be license under its own terms. This is merely considered
 * normal use of OPEN ENT NG, and does not fall under the heading of "covered work".
 *
 * This program 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.
 */

package net.atos.entng.rbs.service;






public class ResourceServiceSqlImpl extends SqlCrudService implements ResourceService {

	public ResourceServiceSqlImpl() {
		super("rbs", "resource");
	}

	@Override
	public void listResources(final List<String> groupsAndUserIds, final UserInfos user,
			final Handler<Either<String, JsonArray>> handler) {

		StringBuilder query = new StringBuilder();
		JsonArray values = new fr.wseduc.webutils.collections.JsonArray();

		query.append("SELECT r.*,")
			.append(" json_agg(row_to_json(row(rs.member_id,rs.action)::rbs.share_tuple)) as shared,")
			.append(" array_to_json(array_agg(m.group_id)) as groups ")
			.append(" FROM rbs.resource AS r")
			.append(" INNER JOIN rbs.resource_type AS t ON r.type_id = t.id")
			.append(" LEFT JOIN rbs.resource_shares AS rs ON r.id = rs.resource_id")
			.append(" LEFT JOIN rbs.resource_type_shares AS ts ON t.id = ts.resource_id")
			.append(" LEFT JOIN rbs.members AS m ON (rs.member_id = m.id AND m.group_id IS NOT NULL)");

		query.append(" WHERE rs.member_id IN ").append(Sql.listPrepared(groupsAndUserIds.toArray()));
		for (String groupOruser : groupsAndUserIds) {
			values.add(groupOruser);
		}

		query.append(" OR r.owner = ? ");
		values.add(user.getUserId());

		query.append(" OR ts.member_id IN ").append(Sql.listPrepared(groupsAndUserIds.toArray()));
		for (String groupOruser : groupsAndUserIds) {
			values.add(groupOruser);
		}

		query.append(" OR t.owner = ?");
		values.add(user.getUserId());

 		// A local administrator of a given school can see all resources of the school's types, even if he is not owner or manager of these types or resources
// Path: src/main/java/net/atos/entng/rbs/BookingUtils.java
// public static List<String> getLocalAdminScope(final UserInfos user) {
// 	Map<String, UserInfos.Function> functions = user.getFunctions();
// 	if (functions != null && functions.containsKey(DefaultFunctions.ADMIN_LOCAL)) {
// 		Function adminLocal = functions.get(DefaultFunctions.ADMIN_LOCAL);
// 		if (adminLocal != null) {
// 			return adminLocal.getScope();
// 		}
// 	}
// 
// 	return new ArrayList<String>();
// }
// 
// Path: src/main/java/net/atos/entng/rbs/BookingStatus.java
// public enum BookingStatus {
// 	CREATED(1), VALIDATED(2), REFUSED(3), SUSPENDED(4);
// 
// 	private final int status;
// 
// 	BookingStatus(int pStatus) {
// 		this.status = pStatus;
// 	}
// 	public int status(){
// 		return status;
// 	}
// }


// Path: src/main/java/net/atos/entng/rbs/service/ResourceServiceSqlImpl.java
import org.entcore.common.service.impl.SqlCrudService;
import org.entcore.common.sql.Sql;
import org.entcore.common.sql.SqlStatementsBuilder;
import org.entcore.common.user.UserInfos;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import fr.wseduc.webutils.Either;
import static net.atos.entng.rbs.BookingStatus.CREATED;
import static net.atos.entng.rbs.BookingStatus.REFUSED;
import static net.atos.entng.rbs.BookingStatus.SUSPENDED;
import static net.atos.entng.rbs.BookingUtils.getLocalAdminScope;
import static org.entcore.common.sql.Sql.parseId;
import static org.entcore.common.sql.SqlResult.*;
import static org.entcore.common.sql.SqlResult.validUniqueResultHandler;
import java.util.List;
import net.atos.entng.rbs.BookingStatus;

/*
 * Copyright © Région Nord Pas de Calais-Picardie,  Département 91, Région Aquitaine-Limousin-Poitou-Charentes, 2016.
 *
 * This file is part of OPEN ENT NG. OPEN ENT NG is a versatile ENT Project based on the JVM and ENT Core Project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation (version 3 of the License).
 *
 * For the sake of explanation, any module that communicate over native
 * Web protocols, such as HTTP, with OPEN ENT NG is outside the scope of this
 * license and could be license under its own terms. This is merely considered
 * normal use of OPEN ENT NG, and does not fall under the heading of "covered work".
 *
 * This program 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.
 */

package net.atos.entng.rbs.service;






public class ResourceServiceSqlImpl extends SqlCrudService implements ResourceService {

	public ResourceServiceSqlImpl() {
		super("rbs", "resource");
	}

	@Override
	public void listResources(final List<String> groupsAndUserIds, final UserInfos user,
			final Handler<Either<String, JsonArray>> handler) {

		StringBuilder query = new StringBuilder();
		JsonArray values = new fr.wseduc.webutils.collections.JsonArray();

		query.append("SELECT r.*,")
			.append(" json_agg(row_to_json(row(rs.member_id,rs.action)::rbs.share_tuple)) as shared,")
			.append(" array_to_json(array_agg(m.group_id)) as groups ")
			.append(" FROM rbs.resource AS r")
			.append(" INNER JOIN rbs.resource_type AS t ON r.type_id = t.id")
			.append(" LEFT JOIN rbs.resource_shares AS rs ON r.id = rs.resource_id")
			.append(" LEFT JOIN rbs.resource_type_shares AS ts ON t.id = ts.resource_id")
			.append(" LEFT JOIN rbs.members AS m ON (rs.member_id = m.id AND m.group_id IS NOT NULL)");

		query.append(" WHERE rs.member_id IN ").append(Sql.listPrepared(groupsAndUserIds.toArray()));
		for (String groupOruser : groupsAndUserIds) {
			values.add(groupOruser);
		}

		query.append(" OR r.owner = ? ");
		values.add(user.getUserId());

		query.append(" OR ts.member_id IN ").append(Sql.listPrepared(groupsAndUserIds.toArray()));
		for (String groupOruser : groupsAndUserIds) {
			values.add(groupOruser);
		}

		query.append(" OR t.owner = ?");
		values.add(user.getUserId());

 		// A local administrator of a given school can see all resources of the school's types, even if he is not owner or manager of these types or resources
		List<String> scope = getLocalAdminScope(user);