package at.arz.latte.rodeo.release.admin;

import java.util.Objects;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import at.arz.latte.rodeo.api.RodeoCommand;
import at.arz.latte.rodeo.infrastructure.RodeoModel;
import at.arz.latte.rodeo.release.Release;
import at.arz.latte.rodeo.release.ReleaseName;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CreateRelease
		implements RodeoCommand<Long> {

	@NotNull
	private ReleaseName releaseName;

	@XmlTransient
	@Inject
	private RodeoModel model;

	CreateRelease() {
		// tool constructor
	}

	public CreateRelease(ReleaseName releaseName) {
		Objects.requireNonNull(releaseName, "releaseName required");
		this.releaseName = releaseName;

	}

	@Override
	public Long execute() {
		Release release = new Release(releaseName);
		model.create(release);
		return release.getId();
	}

	public ReleaseName getReleaseName() {
		return releaseName;
	}
}

@@ -331,16 +331,60 @@
 sient;%0A%0A
+import at.arz.latte.rodeo.api.ObjectExists;%0A
 import a
@@ -419,16 +419,16 @@
 ommand;%0A
-
 import a
@@ -561,16 +561,82 @@
 aseName;
+%0Aimport at.arz.latte.rodeo.release.restapi.FindReleaseByNameQuery;
 %0A%0A@XmlRo
@@ -1080,93 +1080,325 @@
 %7B%0A%09%09
+Find
 Release
- release
+ByNameQuery query
  = new 
+Find
 Release
-(releaseName);%0A%09%09model.create(release);%0A%09%09return release
+ByNameQuery();%0A%09%09query.setReleaseName(getReleaseName());%0A%09%09Release release = model.query(query);%0A%09%09if (release != null) %7B%0A%09%09%09throw new ObjectExists(Release.class, %22releaseName%22, getReleaseName());%0A%09%09%7D%0A%0A%09%09Release r = new Release(releaseName);%0A%09%09model.create(r);%0A%09%09return r
 .get

add check if release already exist