<commit_before>package controllers;

import static models.QMdFormat.mdFormat;
import static models.QMdFormatLabel.mdFormatLabel;
import static models.QMetadata.metadata;
import static models.QStatus.status;
import static models.QStatusLabel.statusLabel;
import static models.QSupplier.supplier;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import com.querydsl.core.Tuple;
import com.querydsl.sql.SQLQuery;

import actions.DefaultAuthenticator;
import models.Delete;
import models.Search;
import models.Supplier;
import play.Routes;
import play.data.DynamicForm;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import util.QueryDSL;
import views.html.*;

@Security.Authenticated(DefaultAuthenticator.class)
public class Index extends Controller {
	@Inject ZooKeeper zk;
	@Inject QueryDSL q;
	
	public Result index(String textSearch, String supplierSearch, String statusSearch, String mdFormatSearch, String dateStartSearch, 
			String dateEndSearch) throws SQLException {
		return q.withTransaction(tx -> {
			List<Tuple> supplierList = tx.select(supplier.all())
	            	.from(supplier)
	            	.orderBy(supplier.name.asc())
	            	.fetch();
	    	
	    	List<Tuple> statusList = tx.select(status.name, statusLabel.label)
	            	.from(status)
	            	.join(statusLabel).on(status.id.eq(statusLabel.statusId))
	            	.fetch();
	    	
	    	List<Tuple> mdFormatList = tx.select(mdFormat.name, mdFormatLabel.label)
	            	.from(mdFormat)
	            	.join(mdFormatLabel).on(mdFormat.id.eq(mdFormatLabel.mdFormatId))
	            	.fetch();
	    	
	    	SimpleDateFormat sdfUS = new SimpleDateFormat("yyyy-MM-dd");
	        SimpleDateFormat sdfLocal = new SimpleDateFormat("dd-MM-yyyy");
			
	        
	        if(textSearch == null && supplierSearch == null && statusSearch == null && mdFormatSearch == null && dateStartSearch == null && dateEndSearch == null) {
	        	List<Tuple> datasetRows = tx.select(metadata.id, metadata.uuid, metadata.title, metadata.lastRevisionDate, statusLabel.label, supplier.name)
		    			.from(metadata)
		    			.join(status).on(metadata.status.eq(status.id))
		    			.join(supplier).on(metadata.supplier.eq(supplier.id))
		    			.join(statusLabel).on(status.id.eq(statusLabel.statusId))
		    			.where(metadata.status.notIn(5))
		    			.limit(200)
		    			.orderBy(metadata.lastRevisionDate.desc())
		    			.fetch();
		    	
		    	return ok(views.html.index.render(datasetRows, supplierList, statusList, mdFormatList, sdfUS, sdfLocal, "", "none", "none", "none", null, null));
	        } else {
	        	SQLQuery<Tuple> datasetQuery = tx.select(metadata.id, metadata.uuid, metadata.title, metadata.lastRevisionDate, statusLabel.label, supplier.name, 
						status.name, mdFormat.name)
		    			.from(metadata)
		    			.join(status).on(metadata.status.eq(status.id))
		    			.join(supplier).on(metadata.supplier.eq(supplier.id))
		    			.join(statusLabel).on(status.id.eq(statusLabel.statusId))
		    			.join(mdFormat).on(metadata.mdFormat.eq(mdFormat.id));
				
				if(!"".equals(textSearch)) {
					datasetQuery
						.where(metadata.title.containsIgnoreCase(textSearch)
								.or(metadata.description.containsIgnoreCase(textSearch)));
				}
				
				if(!"none".equals(supplierSearch)) {
					datasetQuery
						.where(supplier.name.eq(supplierSearch));
				}
				
				if(!"none".equals(statusSearch)) {
					datasetQuery
						.where(status.name.eq(statusSearch));
				}
				
				if(!"none".equals(mdFormatSearch)) {
					datasetQuery
						.where(mdFormat.name.eq(mdFormatSearch));
				}
				
				Date finalDateStartSearch = null;
				if(!"".equals(dateStartSearch)) {
					finalDateStartSearch = sdfUS.parse(dateStartSearch);
				}
				
				Date finalDateEndSearch = null;
				if(!"".equals(dateEndSearch)) {
					finalDateEndSearch = sdfUS.parse(dateEndSearch);
				}
				
				Timestamp timestampStartSearch = null;
				Timestamp timestampEndSearch = null;
				if(finalDateStartSearch != null && finalDateEndSearch != null) {
					timestampStartSearch = new Timestamp(finalDateStartSearch.getTime());
					timestampEndSearch = new Timestamp(finalDateEndSearch.getTime() + 86400000);
					
					datasetQuery
						.where(metadata.lastRevisionDate.after(timestampStartSearch))
						.where(metadata.lastRevisionDate.before(timestampEndSearch));
				}<commit_msg>Suppliers can only see their own records<commit_after>package controllers;

import static models.QMdFormat.mdFormat;
import static models.QMdFormatLabel.mdFormatLabel;
import static models.QMetadata.metadata;
import static models.QStatus.status;
import static models.QStatusLabel.statusLabel;
import static models.QSupplier.supplier;
import static models.QUser.user;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import com.querydsl.core.Tuple;
import com.querydsl.sql.SQLQuery;

import actions.DefaultAuthenticator;
import models.Delete;
import models.Search;
import models.Supplier;
import play.Routes;
import play.data.DynamicForm;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import util.QueryDSL;
import views.html.*;

@Security.Authenticated(DefaultAuthenticator.class)
public class Index extends Controller {
	@Inject ZooKeeper zk;
	@Inject QueryDSL q;
	
	public Result index(String textSearch, String supplierSearch, String statusSearch, String mdFormatSearch, String dateStartSearch, 
			String dateEndSearch) throws SQLException {
		return q.withTransaction(tx -> {
			List<Tuple> supplierList = tx.select(supplier.all())
	            	.from(supplier)
	            	.orderBy(supplier.name.asc())
	            	.fetch();
	    	
	    	List<Tuple> statusList = tx.select(status.name, statusLabel.label)
	            	.from(status)
	            	.join(statusLabel).on(status.id.eq(statusLabel.statusId))
	            	.fetch();
	    	
	    	List<Tuple> mdFormatList = tx.select(mdFormat.name, mdFormatLabel.label)
	            	.from(mdFormat)
	            	.join(mdFormatLabel).on(mdFormat.id.eq(mdFormatLabel.mdFormatId))
	            	.fetch();
	    	
	    	SimpleDateFormat sdfUS = new SimpleDateFormat("yyyy-MM-dd");
	        SimpleDateFormat sdfLocal = new SimpleDateFormat("dd-MM-yyyy");
			
	        
	        if(textSearch == null && supplierSearch == null && statusSearch == null && mdFormatSearch == null && dateStartSearch == null && dateEndSearch == null) {
	        	SQLQuery<Tuple> datasetQuery = tx.select(metadata.id, metadata.uuid, metadata.title, metadata.lastRevisionDate, statusLabel.label, supplier.name)
		    			.from(metadata)
		    			.join(status).on(metadata.status.eq(status.id))
		    			.join(supplier).on(metadata.supplier.eq(supplier.id))
		    			.join(statusLabel).on(status.id.eq(statusLabel.statusId));
	        	
	        	Integer roleId = tx.select(user.roleId)
	        			.from(user)
	        			.where(user.username.eq(session("username")))
	        			.fetchOne();
	        	 
	        	Integer supplierId = tx.select(user.id)
	        			.from(user)
	        			.where(user.username.eq(session("username")))
	        			.fetchOne();
	        	
	        	if(roleId.equals(2)) {
	        		datasetQuery.where(metadata.supplier.eq(supplierId));
	        	}
	        	
	        	List<Tuple> datasetRows = datasetQuery.where(metadata.status.notIn(5))
		    			.limit(200)
		    			.orderBy(metadata.lastRevisionDate.desc())
		    			.fetch();
		    	
		    	return ok(views.html.index.render(datasetRows, supplierList, statusList, mdFormatList, sdfUS, sdfLocal, "", "none", "none", "none", null, null));
	        } else {
	        	SQLQuery<Tuple> datasetQuery = tx.select(metadata.id, metadata.uuid, metadata.title, metadata.lastRevisionDate, statusLabel.label, supplier.name, 
						status.name, mdFormat.name)
		    			.from(metadata)
		    			.join(status).on(metadata.status.eq(status.id))
		    			.join(supplier).on(metadata.supplier.eq(supplier.id))
		    			.join(statusLabel).on(status.id.eq(statusLabel.statusId))
		    			.join(mdFormat).on(metadata.mdFormat.eq(mdFormat.id));
				
				if(!"".equals(textSearch)) {
					datasetQuery
						.where(metadata.title.containsIgnoreCase(textSearch)
								.or(metadata.description.containsIgnoreCase(textSearch)));
				}
				
				if(!"none".equals(supplierSearch)) {
					datasetQuery
						.where(supplier.name.eq(supplierSearch));
				}
				
				if(!"none".equals(statusSearch)) {
					datasetQuery
						.where(status.name.eq(statusSearch));
				}
				
				if(!"none".equals(mdFormatSearch)) {
					datasetQuery
						.where(mdFormat.name.eq(mdFormatSearch));
				}
				
				Date finalDateStartSearch = null;
				if(!"".equals(dateStartSearch)) {
					finalDateStartSearch = sdfUS.parse(dateStartSearch);
				}
				
				Date finalDateEndSearch = null;
				if(!"".equals(dateEndSearch)) {
					finalDateEndSearch = sdfUS.parse(dateEndSearch);
				}
				
				Timestamp timestampStartSearch = null;
				Timestamp timestampEndSearch = null;
				if(finalDateStartSearch != null && finalDateEndSearch != null) {
					timestampStartSearch = new Timestamp(finalDateStartSearch.getTime());
					timestampEndSearch = new Timestamp(finalDateEndSearch.getTime() + 86400000);
					
					datasetQuery
						.where(metadata.lastRevisionDate.after(timestampStartSearch))
						.where(metadata.lastRevisionDate.before(timestampEndSearch));
				}
				
				Integer roleId = tx.select(user.roleId)
	        			.from(user)
	        			.where(user.username.eq(session("username")))
	        			.fetchOne();
	        	 
	        	Integer supplierId = tx.select(user.id)
	        			.from(user)
	        			.where(user.username.eq(session("username")))
	        			.fetchOne();
	        	
	        	if(roleId.equals(2)) {
	        		datasetQuery.where(metadata.supplier.eq(supplierId));
	        	}
{'fraction_non_alphanumeric': 0.12135826771653543, 'fraction_numerical': 0.002559055118110236, 'mean_word_length': 3.098830173457039, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '123607', 'n_tokens_mistral': 3635, 'n_tokens_neox': 3079, 'n_words': 481}