/*
 * Copyright (c) 2016 Open Baton (http://www.openbaton.org)
 *
 * 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.openbaton.catalogue.nfvo;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Version;
import org.openbaton.catalogue.util.IdGenerator;

/** Created by lto on 22/07/15. */
@Entity
public class VNFPackage implements Serializable {

  @Id private String id;
  @Version private int version = 0;

  //Name of the Package
  private String name;

  //NFVO Version
  private String nfvo_version;

  @Override
  public String toString() {
    return "VNFPackage{"
        + "id='"
        + id
        + '\''
        + ", version="
        + version
        + ", name='"
        + name
        + '\''
        + ", nfvo_version='"
        + nfvo_version
        + '\''
        + ", vimTypes='"
        + vimTypes
        + '\''
        + ", imageLink='"
        + imageLink
        + '\''
        + ", scriptsLink='"
        + scriptsLink
        + '\''
        + ", image="
        + image
        + ", scripts="
        + scripts
        + ", projectId='"
        + projectId
        + '\''
        + '}';
  }

  public List<String> getVimTypes() {
    return vimTypes;
  }

  public void setVimTypes(List<String> vimTypes) {
    this.vimTypes = vimTypes;
  }

  @ElementCollection(fetch = FetchType.EAGER)
  private List<String> vimTypes;

  //URL to the image's location
  private String imageLink;

  //URL to the scripts' location
  private String scriptsLink;

  //NFVImage used by this VNFPackage
  @OneToOne(
    fetch = FetchType.EAGER,
    cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST},
    orphanRemoval = true
  )
  private NFVImage image;

  //Set of scripts to execute
  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  private Set<Script> scripts;

  public VNFPackage() {}

  private String projectId;

  public String getProjectId() {
    return projectId;
  }

  public void setProjectId(String projectId) {
    this.projectId = projectId;
  }

  public String getImageLink() {
    return imageLink;
  }

  public void setImageLink(String imageLink) {
    this.imageLink = imageLink;
  }

  @PrePersist
  public void ensureId() {
    id = IdGenerator.createUUID();
  }

  public String getScriptsLink() {
    return scriptsLink;
  }

  public void setScriptsLink(String scriptsLink) {
    this.scriptsLink = scriptsLink;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public Set<Script> getScripts() {
    return scripts;
  }

  public void setScripts(Set<Script> scripts) {
    this.scripts = scripts;
  }

  public int getVersion() {
    return version;
  }

  public void setVersion(int version) {
    this.version = version;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public NFVImage getImage() {
    return image;
  }

  public void setImage(NFVImage image) {
    this.image = image;
  }

  public String getNfvo_version() {
    return nfvo_version;
  }

  public void setNfvo_version(String nfvo_version) {
    this.nfvo_version = nfvo_version;
  }
}

@@ -1367,618 +1367,41 @@
 %0A%0A  
-@Override%0A  public String toString() %7B%0A    return %22VNFPackage%7B%22%0A        + %22id='%22%0A        + id%0A        + '%5C''%0A        + %22, version=%22%0A        + version%0A        + %22, name='%22%0A        + name%0A        + '%5C''%0A        + %22, nfvo_version='%22%0A        + nfvo_version%0A        + '%5C''%0A        + %22, vimTypes='%22%0A        + vimTypes%0A        + '%5C''%0A        + %22, imageLink='%22%0A        + imageLink%0A        + '%5C''%0A        + %22, scriptsLink='%22%0A        + scriptsLink%0A        + '%5C''%0A        + %22, image=%22%0A        + image%0A        + %22, scripts=%22%0A        + scripts%0A        + %22, projectId='%22%0A        + projectId%0A        + '%5C''%0A        + '%7D';%0A  %7D
+private String vnfPackageVersion;
 %0A%0A  
@@ -3440,11 +3440,837 @@
 ion;%0A  %7D
+%0A%0A  public String getVnfPackageVersion() %7B%0A    return vnfPackageVersion;%0A  %7D%0A%0A  public void setVnfPackageVersion(String vnfPackageVersion) %7B%0A    this.vnfPackageVersion = vnfPackageVersion;%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    return %22VNFPackage%7B%22%0A        + %22id='%22%0A        + id%0A        + '%5C''%0A        + %22, name='%22%0A        + name%0A        + '%5C''%0A        + %22, nfvo_version='%22%0A        + nfvo_version%0A        + '%5C''%0A        + %22, vnfPackageVersion='%22%0A        + vnfPackageVersion%0A        + '%5C''%0A        + %22, vimTypes=%22%0A        + vimTypes%0A        + %22, imageLink='%22%0A        + imageLink%0A        + '%5C''%0A        + %22, scriptsLink='%22%0A        + scriptsLink%0A        + '%5C''%0A        + %22, image=%22%0A        + image%0A        + %22, scripts=%22%0A        + scripts%0A        + %22, projectId='%22%0A        + projectId%0A        + '%5C''%0A        + '%7D';%0A  %7D
 %0A%7D%0A

add vnfpackage version