<commit_before>package controllers;

import static models.Domain.from;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import models.Domain;
import models.Domain.Function;
import nl.idgis.publisher.domain.response.Page;
import nl.idgis.publisher.domain.response.Response;
import nl.idgis.publisher.domain.service.CrudOperation;
import nl.idgis.publisher.domain.web.TiledLayer;
import play.Logger;
import play.Play;
import play.data.Form;
import play.data.validation.Constraints;
import play.libs.Akka;
import play.libs.F;
import play.libs.F.Promise;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import views.html.tiledlayers.form;
import views.html.tiledlayers.list;
import actions.DefaultAuthenticator;
import akka.actor.ActorSelection;

@Security.Authenticated (DefaultAuthenticator.class)
public class Tiledlayers extends Controller {

	private final static String databaseRef = Play.application().configuration().getString("publisher.database.actorRef");

	private static Promise<Result> renderCreateForm (final Form<TiledLayerForm> tiledlayerForm) {
		 return Promise.promise(new F.Function0<Result>() {
             @Override
             public Result apply() throws Throwable {
                 return ok (form.render (tiledlayerForm, true));
             }
         });
	}
	
	public static Promise<Result> submitCreateUpdate () {
		final ActorSelection database = Akka.system().actorSelection (databaseRef);
		final Form<TiledLayerForm> form = Form.form (TiledLayerForm.class).bindFromRequest ();
		Logger.debug ("submit TiledLayer: " + form.field("name").value());
		
		// validation
		if (form.hasErrors ()) {
			return renderCreateForm (form);
		}
		
		final TiledLayerForm tiledlayerForm = form.get ();
		final TiledLayer tiledlayer = new TiledLayer(tiledlayerForm.id, "",
				tiledlayerForm.metaWidth, tiledlayerForm.metaHeight, 
				tiledlayerForm.expireCache, tiledlayerForm.expireClients,
				tiledlayerForm.gutter, tiledlayerForm.getMimeFormats()
				);
		
		return from (database)
			.put(tiledlayer)
			.executeFlat (new Function<Response<?>, Promise<Result>> () {
				@Override
				public Promise<Result> apply (final Response<?> response) throws Throwable {
					if (CrudOperation.CREATE.equals (response.getOperation())) {
						Logger.debug ("Created tiledlayer " + tiledlayer);
						flash ("success", Domain.message("web.application.page.tiledlayers.name") + " " + Domain.message("web.application.added").toLowerCase());
					}else{
						Logger.debug ("Updated tiledlayer " + tiledlayer);
						flash ("success", Domain.message("web.application.page.tiledlayers.name") + " " + Domain.message("web.application.updated").toLowerCase());
					}
					return Promise.pure (redirect (routes.Tiledlayers.list ()));
				}
			});
	}
	
	public static Promise<Result> list () {
		final ActorSelection database = Akka.system().actorSelection (databaseRef);

		Logger.debug ("list TiledLayers ");
		
		return from (database)
			.list (TiledLayer.class)
			.execute (new Function<Page<TiledLayer>, Result> () {
				@Override
				public Result apply (final Page<TiledLayer> tiledlayers) throws Throwable {
					return ok (list.render (tiledlayers));
				}
			});
	}

	public static Promise<Result> create () {
		Logger.debug ("create TiledLayer");
		final Form<TiledLayerForm> tiledlayerForm = Form.form (TiledLayerForm.class).fill (new TiledLayerForm ());
		
		return renderCreateForm (tiledlayerForm);
	}
	
	public static Promise<Result> edit (final String tiledlayerId) {
		Logger.debug ("edit TiledLayer: " + tiledlayerId);
		final ActorSelection database = Akka.system().actorSelection (databaseRef);
		
		return from (database)
			.get (TiledLayer.class, tiledlayerId)
			.execute (new Function<TiledLayer, Result> () {

				@Override
				public Result apply (final TiledLayer tiledlayer) throws Throwable {
					final Form<TiledLayerForm> tiledlayerForm = Form
							.form (TiledLayerForm.class)
							.fill (new TiledLayerForm (tiledlayer, tiledlayerId));
					
					Logger.debug ("Edit tiledlayerForm: " + tiledlayerForm);						

					return ok (form.render (tiledlayerForm, false));
				}
			});
	}

	public static Promise<Result> delete(final String tiledlayerId){
		Logger.debug ("delete TiledLayer " + tiledlayerId);
		final ActorSelection database = Akka.system().actorSelection (databaseRef);
		
		from(database).delete(TiledLayer.class, tiledlayerId)
		.execute(new Function<Response<?>, Result>() {
			
			@Override
			public Result apply(Response<?> a) throws Throwable {
				return redirect (routes.Tiledlayers.list ());
			}
		});
		
		return Promise.pure (redirect (routes.Tiledlayers.list ()));
	}
	
	
	public static class TiledLayerForm {
		
		private static final Integer META_WIDTH_DEFAULT = 4;

		private static final Integer META_HEIGTH_DEFAULT = 4;

		private static final Integer EXPIRE_CACHE_DEFAULT = 0;

		private static final Integer EXPIER_CLIENTS_DEFAULT = 0;

		private static final Integer GUTTER_DEFAULT = 0;

		private static final String GIF = "#gif#";
		private static final String JPG = "#jpg#";
		private static final String PNG8 = "#png8#";
		private static final String PNG = "#png#";
		
		private String id;
		private TiledLayer tiledLayer;
		private Boolean png = false;
		private Boolean png8 = false;
		private Boolean jpg = false;
		private Boolean gif = false;
		private Integer metaWidth = META_WIDTH_DEFAULT;
		private Integer metaHeight = META_HEIGTH_DEFAULT;
		private Integer expireCache = EXPIRE_CACHE_DEFAULT;
		private Integer expireClients = EXPIER_CLIENTS_DEFAULT;
		private Integer gutter = GUTTER_DEFAULT;

		
		public TiledLayerForm(){
			super();<commit_msg>using the right mimetype notation<commit_after>package controllers;

import static models.Domain.from;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import models.Domain;
import models.Domain.Function;
import nl.idgis.publisher.domain.response.Page;
import nl.idgis.publisher.domain.response.Response;
import nl.idgis.publisher.domain.service.CrudOperation;
import nl.idgis.publisher.domain.web.TiledLayer;
import play.Logger;
import play.Play;
import play.data.Form;
import play.data.validation.Constraints;
import play.libs.Akka;
import play.libs.F;
import play.libs.F.Promise;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import views.html.tiledlayers.form;
import views.html.tiledlayers.list;
import actions.DefaultAuthenticator;
import akka.actor.ActorSelection;

@Security.Authenticated (DefaultAuthenticator.class)
public class Tiledlayers extends Controller {

	private final static String databaseRef = Play.application().configuration().getString("publisher.database.actorRef");

	private static Promise<Result> renderCreateForm (final Form<TiledLayerForm> tiledlayerForm) {
		 return Promise.promise(new F.Function0<Result>() {
             @Override
             public Result apply() throws Throwable {
                 return ok (form.render (tiledlayerForm, true));
             }
         });
	}
	
	public static Promise<Result> submitCreateUpdate () {
		final ActorSelection database = Akka.system().actorSelection (databaseRef);
		final Form<TiledLayerForm> form = Form.form (TiledLayerForm.class).bindFromRequest ();
		Logger.debug ("submit TiledLayer: " + form.field("name").value());
		
		// validation
		if (form.hasErrors ()) {
			return renderCreateForm (form);
		}
		
		final TiledLayerForm tiledlayerForm = form.get ();
		final TiledLayer tiledlayer = new TiledLayer(tiledlayerForm.id, "",
				tiledlayerForm.metaWidth, tiledlayerForm.metaHeight, 
				tiledlayerForm.expireCache, tiledlayerForm.expireClients,
				tiledlayerForm.gutter, tiledlayerForm.getMimeFormats()
				);
		
		return from (database)
			.put(tiledlayer)
			.executeFlat (new Function<Response<?>, Promise<Result>> () {
				@Override
				public Promise<Result> apply (final Response<?> response) throws Throwable {
					if (CrudOperation.CREATE.equals (response.getOperation())) {
						Logger.debug ("Created tiledlayer " + tiledlayer);
						flash ("success", Domain.message("web.application.page.tiledlayers.name") + " " + Domain.message("web.application.added").toLowerCase());
					}else{
						Logger.debug ("Updated tiledlayer " + tiledlayer);
						flash ("success", Domain.message("web.application.page.tiledlayers.name") + " " + Domain.message("web.application.updated").toLowerCase());
					}
					return Promise.pure (redirect (routes.Tiledlayers.list ()));
				}
			});
	}
	
	public static Promise<Result> list () {
		final ActorSelection database = Akka.system().actorSelection (databaseRef);

		Logger.debug ("list TiledLayers ");
		
		return from (database)
			.list (TiledLayer.class)
			.execute (new Function<Page<TiledLayer>, Result> () {
				@Override
				public Result apply (final Page<TiledLayer> tiledlayers) throws Throwable {
					return ok (list.render (tiledlayers));
				}
			});
	}

	public static Promise<Result> create () {
		Logger.debug ("create TiledLayer");
		final Form<TiledLayerForm> tiledlayerForm = Form.form (TiledLayerForm.class).fill (new TiledLayerForm ());
		
		return renderCreateForm (tiledlayerForm);
	}
	
	public static Promise<Result> edit (final String tiledlayerId) {
		Logger.debug ("edit TiledLayer: " + tiledlayerId);
		final ActorSelection database = Akka.system().actorSelection (databaseRef);
		
		return from (database)
			.get (TiledLayer.class, tiledlayerId)
			.execute (new Function<TiledLayer, Result> () {

				@Override
				public Result apply (final TiledLayer tiledlayer) throws Throwable {
					final Form<TiledLayerForm> tiledlayerForm = Form
							.form (TiledLayerForm.class)
							.fill (new TiledLayerForm (tiledlayer, tiledlayerId));
					
					Logger.debug ("Edit tiledlayerForm: " + tiledlayerForm);						

					return ok (form.render (tiledlayerForm, false));
				}
			});
	}

	public static Promise<Result> delete(final String tiledlayerId){
		Logger.debug ("delete TiledLayer " + tiledlayerId);
		final ActorSelection database = Akka.system().actorSelection (databaseRef);
		
		from(database).delete(TiledLayer.class, tiledlayerId)
		.execute(new Function<Response<?>, Result>() {
			
			@Override
			public Result apply(Response<?> a) throws Throwable {
				return redirect (routes.Tiledlayers.list ());
			}
		});
		
		return Promise.pure (redirect (routes.Tiledlayers.list ()));
	}
	
	
	public static class TiledLayerForm {
		
		private static final Integer META_WIDTH_DEFAULT = 4;

		private static final Integer META_HEIGTH_DEFAULT = 4;

		private static final Integer EXPIRE_CACHE_DEFAULT = 0;

		private static final Integer EXPIER_CLIENTS_DEFAULT = 0;

		private static final Integer GUTTER_DEFAULT = 0;

		private static final String GIF  = "image/gif";
		private static final String JPG  = "image/jpeg";
		private static final String PNG8 = "image/png; mode=8bit";
		private static final String PNG  = "image/png";
		
		private String id;
		private TiledLayer tiledLayer;
		private Boolean png  = false;
		private Boolean png8 = false;
		private Boolean jpg  = false;
		private Boolean gif  = false;
		private Integer metaWidth = META_WIDTH_DEFAULT;
		private Integer metaHeight = META_HEIGTH_DEFAULT;
		private Integer expireCache = EXPIRE_CACHE_DEFAULT;
		private Integer expireClients = EXPIER_CLIENTS_DEFAULT;
		private Integer gutter = GUTTER_DEFAULT;

		
		public TiledLayerForm(){
			super();
{'fraction_non_alphanumeric': 0.10952921652546074, 'fraction_numerical': 0.0015721897108917809, 'mean_word_length': 4.242673992673993, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3623062', 'n_tokens_mistral': 3878, 'n_tokens_neox': 3349, 'n_words': 820}