<commit_before>package gov.nih.nci.cananolab.service.publication.helper;

import gov.nih.nci.cananolab.domain.common.Publication;
import gov.nih.nci.cananolab.domain.particle.Sample;
import gov.nih.nci.cananolab.exception.NoAccessException;
import gov.nih.nci.cananolab.service.BaseServiceHelper;
import gov.nih.nci.cananolab.service.security.SecurityService;
import gov.nih.nci.cananolab.service.security.UserBean;
import gov.nih.nci.cananolab.system.applicationservice.CustomizedApplicationService;
import gov.nih.nci.cananolab.util.Comparators;
import gov.nih.nci.cananolab.util.Constants;
import gov.nih.nci.cananolab.util.StringUtils;
import gov.nih.nci.cananolab.util.TextMatchMode;
import gov.nih.nci.system.client.ApplicationServiceProvider;
import gov.nih.nci.system.query.hibernate.HQLCriteria;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.hibernate.FetchMode;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

/**
 * Helper class providing implementations of search methods needed for both
 * local implementation of PublicationService and grid service *
 * 
 * @author tanq, pansu
 */
public class PublicationServiceHelper extends BaseServiceHelper {
	private Logger logger = Logger.getLogger(PublicationServiceHelper.class);

	public PublicationServiceHelper() {
		super();
	}

	public PublicationServiceHelper(UserBean user) {
		super(user);
	}

	public PublicationServiceHelper(SecurityService securityService) {
		super(securityService);
	}

	public List<String> findPublicationIdsBy(String title, String category,
			String sampleName, String[] researchArea, String[] keywords,
			String pubMedId, String digitalObjectId, String[] authors,
			String[] nanomaterialEntityClassNames,
			String[] otherNanomaterialEntityTypes,
			String[] functionalizingEntityClassNames,
			String[] otherFunctionalizingEntityTypes,
			String[] functionClassNames, String[] otherFunctionTypes)
			throws Exception {

		// Set<String> samplePublicationIds = new HashSet<String>();
		// Set<String> compositionPublicationIds = new HashSet<String>();
		// Set<String> otherPublicationIds = new HashSet<String>();
		// Set<String> allPublicationIds = new HashSet<String>();
		Set<Publication> samplePublications = new HashSet<Publication>();
		Set<Publication> compositionPublications = new HashSet<Publication>();
		Set<Publication> otherPublications = new HashSet<Publication>();
		Set<Publication> allPublications = new HashSet<Publication>();

		// check if sample is accessible
		if (!StringUtils.isEmpty(sampleName)) {
			List<Publication> publications = this
					.findPublicationsBySampleName(sampleName);
			samplePublications.addAll(publications);
			// allPublicationIds.addAll(samplePublicationIds);
			allPublications.addAll(samplePublications);
		}

		if (nanomaterialEntityClassNames != null
				&& nanomaterialEntityClassNames.length > 0
				|| otherNanomaterialEntityTypes != null
				&& otherNanomaterialEntityTypes.length > 0
				|| functionalizingEntityClassNames != null
				&& functionalizingEntityClassNames.length > 0
				|| functionClassNames != null && functionClassNames.length > 0) {
			compositionPublications = this.findPublicationsBySampleComposition(
					sampleName, nanomaterialEntityClassNames,
					otherNanomaterialEntityTypes,
					functionalizingEntityClassNames,
					otherFunctionalizingEntityTypes, functionClassNames,
					otherFunctionTypes);
			// allPublicationIds.addAll(compositionPublicationIds);
			allPublications.addAll(compositionPublications);
		}

		// can't query for the entire Publication object due to limitations in
		// pagination in SDK
		// DetachedCriteria crit = DetachedCriteria.forClass(Publication.class)
		// .setProjection(Projections.distinct(Property.forName("id")));
		//
		DetachedCriteria crit = DetachedCriteria.forClass(Publication.class)
				.setProjection(
						Projections.projectionList()
								.add(Projections.property("id"))
								.add(Projections.property("createdDate")));

		if (!StringUtils.isEmpty(title)) {
			TextMatchMode titleMatchMode = new TextMatchMode(title);
			crit.add(Restrictions.ilike("title",
					titleMatchMode.getUpdatedText(),
					titleMatchMode.getMatchMode()));
		}
		if (!StringUtils.isEmpty(category)) {
			TextMatchMode categoryMatchMode = new TextMatchMode(category);
			crit.add(Restrictions.ilike("category",
					categoryMatchMode.getUpdatedText(),
					categoryMatchMode.getMatchMode()));
		}

		// pubMedId
		if (!StringUtils.isEmpty(pubMedId)) {
			TextMatchMode pubMedIdMatchMode = new TextMatchMode(pubMedId);
			Long pubMedIdLong = null;
			try {
				pubMedIdLong = new Long(pubMedIdMatchMode.getUpdatedText());
			} catch (Exception ex) {
				// ignore
				pubMedIdLong = new Long(0);
			}
			crit.add(Restrictions.eq("pubMedId", pubMedIdLong));
		}
		if (!StringUtils.isEmpty(digitalObjectId)) {
			TextMatchMode digitalObjectIdMatchMode = new TextMatchMode(
					digitalObjectId);
			crit.add(Restrictions.ilike("digitalObjectId",
					digitalObjectIdMatchMode.getUpdatedText(),
					digitalObjectIdMatchMode.getMatchMode()));
		}

		// researchArea
		if (researchArea != null && researchArea.length > 0) {

			Disjunction disjunction = Restrictions.disjunction();
			for (String research : researchArea) {
				Criterion crit1 = Restrictions.like("researchArea", research,
						MatchMode.ANYWHERE);
				disjunction.add(crit1);
			}
			crit.add(disjunction);
		}

		// keywords
		if (keywords != null && keywords.length > 0) {
			Disjunction disjunction = Restrictions.disjunction();
			crit.createAlias("keywordCollection", "keyword");
			for (String keyword : keywords) {
				Criterion keywordCrit1 = Restrictions.ilike("keyword.name",
						keyword, MatchMode.ANYWHERE);
				disjunction.add(keywordCrit1);
			}
			crit.add(disjunction);
		}

		// authors
		if (authors != null && authors.length > 0) {
			Disjunction disjunction = Restrictions.disjunction();
			crit.createAlias("authorCollection", "author");
			for (String author : authors) {
				Criterion crit1 = Restrictions.ilike("author.lastName", author,
						MatchMode.ANYWHERE);
				disjunction.add(crit1);
				Criterion crit2 = Restrictions.ilike("author.firstName",
						author, MatchMode.ANYWHERE);
				disjunction.add(crit2);
				Criterion crit3 = Restrictions.ilike("author.initial", author,
						MatchMode.ANYWHERE);
				disjunction.add(crit3);
			}
			crit.add(disjunction);
		}

		CustomizedApplicationService appService = (CustomizedApplicationService) ApplicationServiceProvider
				.getApplicationService();
		List results = appService.query(crit);
		for (Object obj : results) {
			Object[] row = (Object[]) obj;
			// otherPublicationIds.add(obj.toString());
			Publication publication = new Publication();
			publication.setId((Long) row[0]);
			publication.setCreatedDate((Date) row[1]);
			otherPublications.add(publication);
		}
		// allPublicationIds.addAll(otherPublicationIds);
		allPublications.addAll(otherPublications);

		// find the union of all publication Ids
		// if (!samplePublicationIds.isEmpty()) {
		// allPublicationIds.retainAll(samplePublicationIds);
		// }
		// if (!compositionPublicationIds.isEmpty()) {
		// allPublicationIds.retainAll(compositionPublicationIds);
		// }
		// if (!otherPublicationIds.isEmpty()) {
		// allPublicationIds.retainAll(otherPublicationIds);
		// }
		//
		if (!samplePublications.isEmpty()) {
			allPublications.retainAll(samplePublications);
		}
		if (!compositionPublications.isEmpty()) {
			allPublications.retainAll(compositionPublications);
		}
		if (!otherPublications.isEmpty()) {
			allPublications.retainAll(otherPublications);
		}
		// order publications by createdDate
		List<Publication> orderedPubs = new ArrayList<Publication>(
				allPublications);
		Collections.sort(orderedPubs, Collections
				.reverseOrder(new Comparators.PublicationDateComparator()));
		// get ordered ids
		List<String> orderedPubIds = new ArrayList<String>();
		for (Publication pub : orderedPubs) {
			if (getAccessibleData().contains(pub.getId().toString())) {
				orderedPubIds.add(pub.getId().toString());
			} else {
				// ignore no access exception
				logger.debug("User doesn't have access to publication with id "
						+ pub.getId());
			}
		}
		return orderedPubIds;
	}

	public Publication findPublicationById(String publicationId)
			throws Exception {
		if (!getAccessibleData().contains(publicationId)) {<commit_msg>updated sorting to include publication title<commit_after>package gov.nih.nci.cananolab.service.publication.helper;

import gov.nih.nci.cananolab.domain.common.Publication;
import gov.nih.nci.cananolab.domain.particle.Sample;
import gov.nih.nci.cananolab.exception.NoAccessException;
import gov.nih.nci.cananolab.service.BaseServiceHelper;
import gov.nih.nci.cananolab.service.security.SecurityService;
import gov.nih.nci.cananolab.service.security.UserBean;
import gov.nih.nci.cananolab.system.applicationservice.CustomizedApplicationService;
import gov.nih.nci.cananolab.util.Comparators;
import gov.nih.nci.cananolab.util.Constants;
import gov.nih.nci.cananolab.util.StringUtils;
import gov.nih.nci.cananolab.util.TextMatchMode;
import gov.nih.nci.system.client.ApplicationServiceProvider;
import gov.nih.nci.system.query.hibernate.HQLCriteria;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.hibernate.FetchMode;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

/**
 * Helper class providing implementations of search methods needed for both
 * local implementation of PublicationService and grid service *
 * 
 * @author tanq, pansu
 */
public class PublicationServiceHelper extends BaseServiceHelper {
	private Logger logger = Logger.getLogger(PublicationServiceHelper.class);

	public PublicationServiceHelper() {
		super();
	}

	public PublicationServiceHelper(UserBean user) {
		super(user);
	}

	public PublicationServiceHelper(SecurityService securityService) {
		super(securityService);
	}

	public List<String> findPublicationIdsBy(String title, String category,
			String sampleName, String[] researchArea, String[] keywords,
			String pubMedId, String digitalObjectId, String[] authors,
			String[] nanomaterialEntityClassNames,
			String[] otherNanomaterialEntityTypes,
			String[] functionalizingEntityClassNames,
			String[] otherFunctionalizingEntityTypes,
			String[] functionClassNames, String[] otherFunctionTypes)
			throws Exception {

		// Set<String> samplePublicationIds = new HashSet<String>();
		// Set<String> compositionPublicationIds = new HashSet<String>();
		// Set<String> otherPublicationIds = new HashSet<String>();
		// Set<String> allPublicationIds = new HashSet<String>();
		Set<Publication> samplePublications = new HashSet<Publication>();
		Set<Publication> compositionPublications = new HashSet<Publication>();
		Set<Publication> otherPublications = new HashSet<Publication>();
		Set<Publication> allPublications = new HashSet<Publication>();

		// check if sample is accessible
		if (!StringUtils.isEmpty(sampleName)) {
			List<Publication> publications = this
					.findPublicationsBySampleName(sampleName);
			samplePublications.addAll(publications);
			// allPublicationIds.addAll(samplePublicationIds);
			allPublications.addAll(samplePublications);
		}

		if (nanomaterialEntityClassNames != null
				&& nanomaterialEntityClassNames.length > 0
				|| otherNanomaterialEntityTypes != null
				&& otherNanomaterialEntityTypes.length > 0
				|| functionalizingEntityClassNames != null
				&& functionalizingEntityClassNames.length > 0
				|| functionClassNames != null && functionClassNames.length > 0) {
			compositionPublications = this.findPublicationsBySampleComposition(
					sampleName, nanomaterialEntityClassNames,
					otherNanomaterialEntityTypes,
					functionalizingEntityClassNames,
					otherFunctionalizingEntityTypes, functionClassNames,
					otherFunctionTypes);
			// allPublicationIds.addAll(compositionPublicationIds);
			allPublications.addAll(compositionPublications);
		}

		// can't query for the entire Publication object due to limitations in
		// pagination in SDK
		// DetachedCriteria crit = DetachedCriteria.forClass(Publication.class)
		// .setProjection(Projections.distinct(Property.forName("id")));
		//
		// added created date and title to allow sorting on date and title
		DetachedCriteria crit = DetachedCriteria.forClass(Publication.class)
				.setProjection(
						Projections.projectionList()
								.add(Projections.property("id"))
								.add(Projections.property("title"))
								.add(Projections.property("createdDate")));

		if (!StringUtils.isEmpty(title)) {
			TextMatchMode titleMatchMode = new TextMatchMode(title);
			crit.add(Restrictions.ilike("title",
					titleMatchMode.getUpdatedText(),
					titleMatchMode.getMatchMode()));
		}
		if (!StringUtils.isEmpty(category)) {
			TextMatchMode categoryMatchMode = new TextMatchMode(category);
			crit.add(Restrictions.ilike("category",
					categoryMatchMode.getUpdatedText(),
					categoryMatchMode.getMatchMode()));
		}

		// pubMedId
		if (!StringUtils.isEmpty(pubMedId)) {
			TextMatchMode pubMedIdMatchMode = new TextMatchMode(pubMedId);
			Long pubMedIdLong = null;
			try {
				pubMedIdLong = new Long(pubMedIdMatchMode.getUpdatedText());
			} catch (Exception ex) {
				// ignore
				pubMedIdLong = new Long(0);
			}
			crit.add(Restrictions.eq("pubMedId", pubMedIdLong));
		}
		if (!StringUtils.isEmpty(digitalObjectId)) {
			TextMatchMode digitalObjectIdMatchMode = new TextMatchMode(
					digitalObjectId);
			crit.add(Restrictions.ilike("digitalObjectId",
					digitalObjectIdMatchMode.getUpdatedText(),
					digitalObjectIdMatchMode.getMatchMode()));
		}

		// researchArea
		if (researchArea != null && researchArea.length > 0) {

			Disjunction disjunction = Restrictions.disjunction();
			for (String research : researchArea) {
				Criterion crit1 = Restrictions.like("researchArea", research,
						MatchMode.ANYWHERE);
				disjunction.add(crit1);
			}
			crit.add(disjunction);
		}

		// keywords
		if (keywords != null && keywords.length > 0) {
			Disjunction disjunction = Restrictions.disjunction();
			crit.createAlias("keywordCollection", "keyword");
			for (String keyword : keywords) {
				Criterion keywordCrit1 = Restrictions.ilike("keyword.name",
						keyword, MatchMode.ANYWHERE);
				disjunction.add(keywordCrit1);
			}
			crit.add(disjunction);
		}

		// authors
		if (authors != null && authors.length > 0) {
			Disjunction disjunction = Restrictions.disjunction();
			crit.createAlias("authorCollection", "author");
			for (String author : authors) {
				Criterion crit1 = Restrictions.ilike("author.lastName", author,
						MatchMode.ANYWHERE);
				disjunction.add(crit1);
				Criterion crit2 = Restrictions.ilike("author.firstName",
						author, MatchMode.ANYWHERE);
				disjunction.add(crit2);
				Criterion crit3 = Restrictions.ilike("author.initial", author,
						MatchMode.ANYWHERE);
				disjunction.add(crit3);
			}
			crit.add(disjunction);
		}

		CustomizedApplicationService appService = (CustomizedApplicationService) ApplicationServiceProvider
				.getApplicationService();
		List results = appService.query(crit);
		for (Object obj : results) {
			Object[] row = (Object[]) obj;
			// otherPublicationIds.add(obj.toString());
			Publication publication = new Publication();
			publication.setId((Long) row[0]);
			publication.setTitle((String) row[1]);
			publication.setCreatedDate((Date) row[2]);
			otherPublications.add(publication);
		}
		// allPublicationIds.addAll(otherPublicationIds);
		allPublications.addAll(otherPublications);

		// find the union of all publication Ids
		// if (!samplePublicationIds.isEmpty()) {
		// allPublicationIds.retainAll(samplePublicationIds);
		// }
		// if (!compositionPublicationIds.isEmpty()) {
		// allPublicationIds.retainAll(compositionPublicationIds);
		// }
		// if (!otherPublicationIds.isEmpty()) {
		// allPublicationIds.retainAll(otherPublicationIds);
		// }
		//
		if (!samplePublications.isEmpty()) {
			allPublications.retainAll(samplePublications);
		}
		if (!compositionPublications.isEmpty()) {
			allPublications.retainAll(compositionPublications);
		}
		if (!otherPublications.isEmpty()) {
			allPublications.retainAll(otherPublications);
		}
		// order publications by createdDate
		List<Publication> orderedPubs = new ArrayList<Publication>(
				allPublications);
		Collections.sort(orderedPubs,
				new Comparators.PublicationDateComparator());
		// get ordered ids
		List<String> orderedPubIds = new ArrayList<String>();
		for (Publication pub : orderedPubs) {
			if (getAccessibleData().contains(pub.getId().toString())) {
				orderedPubIds.add(pub.getId().toString());
			} else {
				// ignore no access exception
				logger.debug("User doesn't have access to publication with id "
						+ pub.getId());
			}
		}
		return orderedPubIds;
	}

	public Publication findPublicationById(String publicationId)
			throws Exception {
		if (!getAccessibleData().contains(publicationId)) {
{'fraction_non_alphanumeric': 0.11220374564941163, 'fraction_numerical': 0.0023755593613612506, 'mean_word_length': 4.931192660550459, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 61, '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': '6445890', 'n_tokens_mistral': 6150, 'n_tokens_neox': 5209, 'n_words': 959}