<commit_before>/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 org.jboss.pnc.model;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.hibernate.envers.NotAudited;
import org.hibernate.envers.RelationTargetAuditMode;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceException;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.SequenceGenerator;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * The Class BuildConfiguration cointains the information needed to execute a build of a project, i.e. the sources,
 * the build script, the build system image needed to run, the project configurations that need to be triggered after
 * a successful build. Note that creationTime and lastModificationTime are handled internally via JPA settings
 * and therefore do not have public setters.
 * <p>
 * (project + name) should be unique
 *
 * @author avibelli
 */
@Entity
@Audited
public class BuildConfiguration implements GenericEntity<Integer>, Cloneable {

    private static final long serialVersionUID = -5890729679489304114L;

    public static final String DEFAULT_SORTING_FIELD = "name";
    public static final String SEQUENCE_NAME = "build_configuration_id_seq";

    @Id
    @SequenceGenerator(name = SEQUENCE_NAME, sequenceName = SEQUENCE_NAME, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_NAME)
    private Integer id;

    @Column(unique = true)
    @NotNull
    private String name;

    @Lob
    private String buildScript;

    /**
     * The upstream/community scm repo URL submitted by the user.
     */
    private String scmRepoURL;

    /**
     * The upstream/community scm revision (commit ID/tag/branch) submitted by the user.
     */
    private String scmRevision;

    /**
     * The URL of the internal mirror of the upstream repository. For builds which require the sources to be mirrored to a
     * secured location before building.
     */
    private String scmMirrorRepoURL;

    /**
     * The SCM revision of the internal mirror of the upstream repository. Contains the revision after any automated source
     * changes have been made by the build system.
     */
    private String scmMirrorRevision;

    @Lob
    @Type(type = "org.hibernate.type.TextType")
    private String description;

    @NotAudited
    @ManyToOne
    @ForeignKey(name = "fk_build_configuration_product_version")
    @Index(name="idx_build_configuration_product_version")
    private ProductVersion productVersion;

    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    @NotNull
    @ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.DETACH })
    @ForeignKey(name = "fk_buildconfiguration_project")
    @Index(name="idx_buildconfiguration_project")
    private Project project;

    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    @NotNull
    @ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.DETACH })
    @ForeignKey(name = "fk_buildconfiguration_buildenvironment")
    @Index(name="idx_buildconfiguration_buildenvironment")
    private BuildEnvironment buildEnvironment;

    @NotAudited
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "latestBuildConfiguration")
    private Set<BuildRecord> buildRecords;

    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    @ManyToMany(mappedBy = "buildConfigurations")
    private Set<BuildConfigurationSet> buildConfigurationSets;

    @NotNull
    @Column(columnDefinition = "timestamp with time zone", updatable=false)
    private Date creationTime;

    /**
     * The time at which this entity was last modified and saved to the database.  This is automatically
     * managed by JPA.
     */
    @NotNull
    @Version
    @Column(columnDefinition = "timestamp with time zone")
    private Date lastModificationTime;

    /**
     * If true, indicates that this build configuration should no longer be used.
     * An archived build configuration should normally not be included in search 
     * results.  It should only be visible if the user is looking for that specific
     * configuration.  Defaults to false.
     */<commit_msg>[NCL-2136] Don't cascade changes from build configuration to build record<commit_after>/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 org.jboss.pnc.model;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.hibernate.envers.NotAudited;
import org.hibernate.envers.RelationTargetAuditMode;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceException;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.SequenceGenerator;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * The Class BuildConfiguration cointains the information needed to execute a build of a project, i.e. the sources,
 * the build script, the build system image needed to run, the project configurations that need to be triggered after
 * a successful build. Note that creationTime and lastModificationTime are handled internally via JPA settings
 * and therefore do not have public setters.
 * <p>
 * (project + name) should be unique
 *
 * @author avibelli
 */
@Entity
@Audited
public class BuildConfiguration implements GenericEntity<Integer>, Cloneable {

    private static final long serialVersionUID = -5890729679489304114L;

    public static final String DEFAULT_SORTING_FIELD = "name";
    public static final String SEQUENCE_NAME = "build_configuration_id_seq";

    @Id
    @SequenceGenerator(name = SEQUENCE_NAME, sequenceName = SEQUENCE_NAME, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_NAME)
    private Integer id;

    @Column(unique = true)
    @NotNull
    private String name;

    @Lob
    private String buildScript;

    /**
     * The upstream/community scm repo URL submitted by the user.
     */
    private String scmRepoURL;

    /**
     * The upstream/community scm revision (commit ID/tag/branch) submitted by the user.
     */
    private String scmRevision;

    /**
     * The URL of the internal mirror of the upstream repository. For builds which require the sources to be mirrored to a
     * secured location before building.
     */
    private String scmMirrorRepoURL;

    /**
     * The SCM revision of the internal mirror of the upstream repository. Contains the revision after any automated source
     * changes have been made by the build system.
     */
    private String scmMirrorRevision;

    @Lob
    @Type(type = "org.hibernate.type.TextType")
    private String description;

    @NotAudited
    @ManyToOne
    @ForeignKey(name = "fk_build_configuration_product_version")
    @Index(name="idx_build_configuration_product_version")
    private ProductVersion productVersion;

    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    @NotNull
    @ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.DETACH })
    @ForeignKey(name = "fk_buildconfiguration_project")
    @Index(name="idx_buildconfiguration_project")
    private Project project;

    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    @NotNull
    @ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.DETACH })
    @ForeignKey(name = "fk_buildconfiguration_buildenvironment")
    @Index(name="idx_buildconfiguration_buildenvironment")
    private BuildEnvironment buildEnvironment;

    @NotAudited
    @OneToMany(cascade = CascadeType.REFRESH, mappedBy = "latestBuildConfiguration")
    private Set<BuildRecord> buildRecords;

    @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
    @ManyToMany(mappedBy = "buildConfigurations")
    private Set<BuildConfigurationSet> buildConfigurationSets;

    @NotNull
    @Column(columnDefinition = "timestamp with time zone", updatable=false)
    private Date creationTime;

    /**
     * The time at which this entity was last modified and saved to the database.  This is automatically
     * managed by JPA.
     */
    @NotNull
    @Version
    @Column(columnDefinition = "timestamp with time zone")
    private Date lastModificationTime;

    /**
     * If true, indicates that this build configuration should no longer be used.
     * An archived build configuration should normally not be included in search 
     * results.  It should only be visible if the user is looking for that specific
     * configuration.  Defaults to false.
     */
{'fraction_non_alphanumeric': 0.07558644656820157, 'fraction_numerical': 0.005212858384013901, 'mean_word_length': 4.277854195323246, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1692845', 'n_tokens_mistral': 3188, 'n_tokens_neox': 3073, 'n_words': 1013}