<commit_before>/*
 * The aspiredb project
 * 
 * Copyright (c) 2012 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package ubc.pavlab.aspiredb.server.model;

import gemma.gsec.model.Securable;
import gemma.gsec.model.SecuredChild;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.security.access.annotation.Secured;

import ubc.pavlab.aspiredb.server.util.PhenotypeUtil;
import ubc.pavlab.aspiredb.shared.PhenotypeValueObject;
import ubc.pavlab.aspiredb.shared.SubjectValueObject;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@BatchSize(size = 100)
@Table(name = "SUBJECT")
public class Subject implements Serializable, SecuredChild {

    private static final long serialVersionUID = -7549951725408353980L;

    public static SubjectValueObject convertToValueObject( Subject subject ) {
        SubjectValueObject valueObject = new SubjectValueObject();
        valueObject.setId( subject.getId() );
        valueObject.setPatientId( subject.getPatientId() );
        valueObject.setLabels( Label.toValueObjects( subject.getLabels() ) );
        return valueObject;
    }

    public static Collection<Subject> emptyCollection() {
        return new ArrayList<Subject>();
    }

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @Column(name = "PATIENT_ID")
    private String patientId;

    @ManyToMany
    @JoinTable(name = "SUBJECT_PROJECTS", joinColumns = { @JoinColumn(name = "SUBJECT_ID", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "PROJECT_ID", referencedColumnName = "ID") })
    private List<Project> projects = new ArrayList<Project>();

    @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "subject", orphanRemoval = true)
    private Collection<Phenotype> phenotypes = new HashSet<Phenotype>();

    @OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true)
    @JoinTable(name = "SUBJECT_VARIANT", joinColumns = { @JoinColumn(name = "SUBJECT_ID", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "VARIANT_ID", referencedColumnName = "ID") })
    private List<Variant> variants;

    @ManyToMany
    @JoinTable(name = "SUBJECT_LABEL", joinColumns = { @JoinColumn(name = "SUBJECT_FK", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "LABEL_FK", referencedColumnName = "ID") })
    private Collection<Label> labels = new HashSet<Label>();

    public Subject() {
        variants = new ArrayList<Variant>();
    }

    public void addLabel( Label label ) {<commit_msg>Removes redundant join table, see Bug 4374<commit_after>/*
 * The aspiredb project
 * 
 * Copyright (c) 2012 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package ubc.pavlab.aspiredb.server.model;

import gemma.gsec.model.Securable;
import gemma.gsec.model.SecuredChild;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.security.access.annotation.Secured;

import ubc.pavlab.aspiredb.server.util.PhenotypeUtil;
import ubc.pavlab.aspiredb.shared.PhenotypeValueObject;
import ubc.pavlab.aspiredb.shared.SubjectValueObject;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@BatchSize(size = 100)
@Table(name = "SUBJECT")
public class Subject implements Serializable, SecuredChild {

    private static final long serialVersionUID = -7549951725408353980L;

    public static SubjectValueObject convertToValueObject( Subject subject ) {
        SubjectValueObject valueObject = new SubjectValueObject();
        valueObject.setId( subject.getId() );
        valueObject.setPatientId( subject.getPatientId() );
        valueObject.setLabels( Label.toValueObjects( subject.getLabels() ) );
        return valueObject;
    }

    public static Collection<Subject> emptyCollection() {
        return new ArrayList<Subject>();
    }

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @Column(name = "PATIENT_ID")
    private String patientId;

    @ManyToMany
    @JoinTable(name = "SUBJECT_PROJECTS", joinColumns = { @JoinColumn(name = "SUBJECT_ID", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "PROJECT_ID", referencedColumnName = "ID") })
    private List<Project> projects = new ArrayList<Project>();

    @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "subject", orphanRemoval = true)
    private Collection<Phenotype> phenotypes = new HashSet<Phenotype>();

    @OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true, mappedBy="subject")
    //@JoinTable(name = "SUBJECT_VARIANT", joinColumns = { @JoinColumn(name = "SUBJECT_ID", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "VARIANT_ID", referencedColumnName = "ID") })
    private List<Variant> variants;

    @ManyToMany
    @JoinTable(name = "SUBJECT_LABEL", joinColumns = { @JoinColumn(name = "SUBJECT_FK", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "LABEL_FK", referencedColumnName = "ID") })
    private Collection<Label> labels = new HashSet<Label>();

    public Subject() {
        variants = new ArrayList<Variant>();
    }

    public void addLabel( Label label ) {
{'fraction_non_alphanumeric': 0.10528428093645485, 'fraction_numerical': 0.008561872909698997, 'mean_word_length': 4.786377708978328, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6036141', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2109, 'n_words': 559}