package org.col.resources;

import java.util.List;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.col.api.model.Dataset;
import org.col.api.model.DatasetImport;
import org.col.api.model.Page;
import org.col.api.model.ResultPage;
import org.col.api.search.DatasetSearchRequest;
import org.col.api.vocab.ImportState;
import org.col.db.dao.DatasetDao;
import org.col.db.dao.DatasetImportDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Path("/dataset")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("static-method")
public class DatasetResource {

  @SuppressWarnings("unused")
  private static final Logger LOG = LoggerFactory.getLogger(DatasetResource.class);
  private final SqlSessionFactory factory;

  public DatasetResource(SqlSessionFactory factory) {
    this.factory = factory;
  }

  @GET
  public ResultPage<Dataset> list(@Valid @BeanParam Page page, @BeanParam DatasetSearchRequest req,
                                  @Context SqlSession session) {
    return new DatasetDao(session).search(req, page);
  }

  @POST
  public Integer create(Dataset dataset, @Context SqlSession session) {
    return new DatasetDao(session).create(dataset);
  }

  @GET
  @Path("{key}")
  public Dataset get(@PathParam("key") int key, @Context SqlSession session) {
    return new DatasetDao(session).get(key);
  }

  @PUT
  @Path("{key}")
  public void update(Dataset dataset, @Context SqlSession session) {
    new DatasetDao(session).update(dataset);
  }

  @DELETE
  @Path("{key}")
  public void delete(@PathParam("key") Integer key, @Context SqlSession session) {
    new DatasetDao(session).delete(key);
  }

  @GET
  @Path("{key}/import")
  public List<DatasetImport> getImports(@PathParam("key") int key,
                                        @QueryParam("limit") @DefaultValue("1") int limit,
                                        @QueryParam("state") ImportState state) {
    return new DatasetImportDao(factory).listByDataset(key, state, limit);
  }

}

@@ -21,16 +21,37 @@
 urces;%0A%0A
+import java.net.URI;%0A
 import j
@@ -96,16 +96,61 @@
 .Valid;%0A
+import javax.validation.constraints.NotNull;%0A
 import j
@@ -515,24 +515,99 @@
 rchRequest;%0A
+import org.col.api.vocab.DataFormat;%0Aimport org.col.api.vocab.DatasetType;%0A
 import org.c
@@ -1494,24 +1494,550 @@
 ataset);%0A  %7D
+%0A  %0A  @POST%0A  @Path(%22register%22)%0A  public Integer register(@Context SqlSession session,%0A                          @QueryParam(%22url%22) @NotNull String url,%0A                          @QueryParam(%22format%22) @DefaultValue(%22ACEF%22) DataFormat format,%0A                          @QueryParam(%22type%22) @DefaultValue(%22OTHER%22) DatasetType type%0A                          ) %7B%0A    Dataset d = new Dataset();%0A    d.setType(type);%0A    d.setDataFormat(format);%0A    d.setDataAccess(URI.create(url));%0A    return new DatasetDao(session).create(d);%0A  %7D
 %0A%0A  @GET%0A  @

Add register dataset convenience method