// Path: app/utils/BasicRequests.java
// public class BasicRequests {
// 
// 	public static class deleteRequest {
// 		public Long id;
// 		public deleteRequest() {}
// 	}
// 	public static class deleteRequestMongoModel {
// 		public String id;
// 		public deleteRequestMongoModel() {}
// 	}
// 
// 	public static class filterRequest {
// 		public String term;
// 	}
// 	
// 	public static class listingRequest {
// 		public Integer p;
// 		public String list_order_by;
// 		public String order_dir = "";
// 		public Byte resultsPerPage;
// 		
// 		protected Integer totalResults = 0;
// 		protected Integer totalPages = 0;
// 		protected Byte paginationSectionSize = 6;
// 		
// 		public listingRequest() {
// 			p = 1;
// 			resultsPerPage = 25;
// 		}
// 		
// 		public void setTotalResults(Integer results) {
// 			totalResults = results;
// 			this.totalPages = (int)( Math.ceil( (double)totalResults / (double)resultsPerPage  ) );
// 		}
// 		public Integer getTotalPages() {
// 			return totalPages;
// 		}
// 		public Integer getTotalResults() {
// 			return totalResults;
// 		}
// 		public int getNextSetOfPages() {
// 			if( this.getTotalPages() - p > paginationSectionSize ) 
// 				return p >= paginationSectionSize ? p + paginationSectionSize : ( paginationSectionSize + 3 <= this.getTotalPages() ? paginationSectionSize + 3 : this.getTotalPages() );
// 			return this.getTotalPages();
// 			
// 		}
// 		public int getFirstSetOfPages() {
// 			if( p - paginationSectionSize >= 1 ) return p - paginationSectionSize;
// 			else return 1;
// 		}
// 		public int getResultsPerPage() {
// 			return resultsPerPage;
// 		}
// 		public Integer getCurrentPage () {
// 			return p;
// 		}
// 	}
// 	
// 	
// 	
// }
// 
// Path: app/models/Administrator.java
// @MongoCollection(name = "administrators")
// public class Administrator {
// 
// 	public static JacksonDBCollection<Administrator.Model, String> coll = MongoDB.getCollection("administrators", Administrator.Model.class, String.class);
// 	public static class Model {
// 		@ObjectId
// 		@Id
// 		public String _id;
// 		
// 		public Date createdAt;
// 		public Date lastLoginAt;
// 		public Date lastUpdatedAt;
// 		
// 		@Required
// 		public String username;
// 		public String password;
// 	}
// 	
// 
// 	public static WriteResult<Administrator.Model, String> save(Administrator.Model ob) {
// 		return coll.save( ob );
// 	}
// 	public static WriteResult<Administrator.Model, String> update(String id, Administrator.Model ob) {
// 		return coll.updateById(id , ob);
// 	}
// }


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.bson.types.ObjectId;
import com.mongodb.DBCursor;
import net.vz.mongodb.jackson.DBQuery;
import net.vz.mongodb.jackson.DBQuery.Query;
import ch.qos.logback.core.Context;
import utils.BasicRequests;
import models.Administrator;
import play.data.Form;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;
import play.mvc.With;
import views.html.administrators.*;
import static play.libs.Json.toJson;
import play.mvc.Security;
		ret.put("ret_code", "0");
		ret.put("id", ""+req.get().id);
		return ok( toJson( ret ) );
	}
	
	public static Result save() {
		Form<Administrator.Model> formAdmin = form(Administrator.Model.class).bindFromRequest();
		
		if( formAdmin.field("username").value().isEmpty() ) formAdmin.reject("username", "Username can not be empty!");
		if( formAdmin.hasErrors() ) {
			flash().put("form_error", "Please fill in all fields properly");
			formAdmin.reject("username", "Required");
		}
		
		if( !formAdmin.field("id").valueOr("").isEmpty() && Administrator.coll.find( DBQuery.is("username", formAdmin.field("username").valueOr("").trim() ).notEquals("_id", new ObjectId( formAdmin.field("id").valueOr("") ) ) ).count() > 0  ) {
			flash().put("form_error", "Username is taken, please provide a unique one!");
			flash().put("error_username", "Username is taken!");
			formAdmin.reject("username", "Username is taken!");
//			return badRequest( views.html.administrators.form.render( formAdmin.field("id").valueOr("").isEmpty() ? "New Administrator" : "Edit Administrator", formAdmin) );
		}
		if( !formAdmin.field("_id").valueOr("").isEmpty() ) {
			if( !formAdmin.field("password").value().equals( formAdmin.field("password_confirm").value() ) ) {
				flash().put("form_error", "Provided passwords must match!!");
				flash().put("error_password", "Passwords must match!");
				formAdmin.reject("password", "Passwords must match!");
			}
		} else {
			if( !formAdmin.field("password").value().equals( formAdmin.field("password_confirm").value() ) || formAdmin.field("password").valueOr("").isEmpty() ) {
				flash().put("form_error", "Provided passwords must match!!");
				flash().put("error_password", "Passwords must match!");
// Path: app/utils/BasicRequests.java
// public class BasicRequests {
// 
// 	public static class deleteRequest {
// 		public Long id;
// 		public deleteRequest() {}
// 	}
// 	public static class deleteRequestMongoModel {
// 		public String id;
// 		public deleteRequestMongoModel() {}
// 	}
// 
// 	public static class filterRequest {
// 		public String term;
// 	}
// 	
// 	public static class listingRequest {
// 		public Integer p;
// 		public String list_order_by;
// 		public String order_dir = "";
// 		public Byte resultsPerPage;
// 		
// 		protected Integer totalResults = 0;
// 		protected Integer totalPages = 0;
// 		protected Byte paginationSectionSize = 6;
// 		
// 		public listingRequest() {
// 			p = 1;
// 			resultsPerPage = 25;
// 		}
// 		
// 		public void setTotalResults(Integer results) {
// 			totalResults = results;
// 			this.totalPages = (int)( Math.ceil( (double)totalResults / (double)resultsPerPage  ) );
// 		}
// 		public Integer getTotalPages() {
// 			return totalPages;
// 		}
// 		public Integer getTotalResults() {
// 			return totalResults;
// 		}
// 		public int getNextSetOfPages() {
// 			if( this.getTotalPages() - p > paginationSectionSize ) 
// 				return p >= paginationSectionSize ? p + paginationSectionSize : ( paginationSectionSize + 3 <= this.getTotalPages() ? paginationSectionSize + 3 : this.getTotalPages() );
// 			return this.getTotalPages();
// 			
// 		}
// 		public int getFirstSetOfPages() {
// 			if( p - paginationSectionSize >= 1 ) return p - paginationSectionSize;
// 			else return 1;
// 		}
// 		public int getResultsPerPage() {
// 			return resultsPerPage;
// 		}
// 		public Integer getCurrentPage () {
// 			return p;
// 		}
// 	}
// 	
// 	
// 	
// }
// 
// Path: app/models/Administrator.java
// @MongoCollection(name = "administrators")
// public class Administrator {
// 
// 	public static JacksonDBCollection<Administrator.Model, String> coll = MongoDB.getCollection("administrators", Administrator.Model.class, String.class);
// 	public static class Model {
// 		@ObjectId
// 		@Id
// 		public String _id;
// 		
// 		public Date createdAt;
// 		public Date lastLoginAt;
// 		public Date lastUpdatedAt;
// 		
// 		@Required
// 		public String username;
// 		public String password;
// 	}
// 	
// 
// 	public static WriteResult<Administrator.Model, String> save(Administrator.Model ob) {
// 		return coll.save( ob );
// 	}
// 	public static WriteResult<Administrator.Model, String> update(String id, Administrator.Model ob) {
// 		return coll.updateById(id , ob);
// 	}
// }


// Path: app/controllers/Administrators.java
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.bson.types.ObjectId;
import com.mongodb.DBCursor;
import net.vz.mongodb.jackson.DBQuery;
import net.vz.mongodb.jackson.DBQuery.Query;
import ch.qos.logback.core.Context;
import utils.BasicRequests;
import models.Administrator;
import play.data.Form;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;
import play.mvc.With;
import views.html.administrators.*;
import static play.libs.Json.toJson;
import play.mvc.Security;

		ret.put("ret_code", "0");
		ret.put("id", ""+req.get().id);
		return ok( toJson( ret ) );
	}
	
	public static Result save() {
		Form<Administrator.Model> formAdmin = form(Administrator.Model.class).bindFromRequest();
		
		if( formAdmin.field("username").value().isEmpty() ) formAdmin.reject("username", "Username can not be empty!");
		if( formAdmin.hasErrors() ) {
			flash().put("form_error", "Please fill in all fields properly");
			formAdmin.reject("username", "Required");
		}
		
		if( !formAdmin.field("id").valueOr("").isEmpty() && Administrator.coll.find( DBQuery.is("username", formAdmin.field("username").valueOr("").trim() ).notEquals("_id", new ObjectId( formAdmin.field("id").valueOr("") ) ) ).count() > 0  ) {
			flash().put("form_error", "Username is taken, please provide a unique one!");
			flash().put("error_username", "Username is taken!");
			formAdmin.reject("username", "Username is taken!");
//			return badRequest( views.html.administrators.form.render( formAdmin.field("id").valueOr("").isEmpty() ? "New Administrator" : "Edit Administrator", formAdmin) );
		}
		if( !formAdmin.field("_id").valueOr("").isEmpty() ) {
			if( !formAdmin.field("password").value().equals( formAdmin.field("password_confirm").value() ) ) {
				flash().put("form_error", "Provided passwords must match!!");
				flash().put("error_password", "Passwords must match!");
				formAdmin.reject("password", "Passwords must match!");
			}
		} else {
			if( !formAdmin.field("password").value().equals( formAdmin.field("password_confirm").value() ) || formAdmin.field("password").valueOr("").isEmpty() ) {
				flash().put("form_error", "Provided passwords must match!!");
				flash().put("error_password", "Passwords must match!");
				formAdmin.reject("password", "Passwords must match!");