package com.obductiongame.translate.server.dao;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.obductiongame.translate.server.ServiceException;
import com.obductiongame.translate.server.ValidationException;
import com.obductiongame.translate.server.entity.DialogueLine;

public class DialogueLineDao extends EntityDao<DialogueLine> {

	private static final Logger LOG = Logger.getLogger(DialogueLineDao.class.getName());
// TODO: use id + lang as key to find entity faster
	public DialogueLineDao() {
		super(DialogueLine.class);
	}

	@Override
	public void put(DialogueLine line) throws ValidationException {
		/*if (Arrays.binarySearch(LanguageLoader.getLanguages(), new LanguageImpl(line.getLanguage())) < 0) {
			LOG.log(Level.SEVERE, "The dialogue line has an invalid language code.");
			throw new ValidationException("The dialogue line has an invalid language code.");
		}*/ // TODO: figure out why this does not always work

		PersistenceManager pm = PMF.getPersistenceManager(); // TODO: cover to transaction as per google jdo pages
		try {//https://groups.google.com/forum/#!topic/objectify-appengine/Jamr_Ib4vlE
			Query q = pm.newQuery("select key from " + DialogueLine.class.getName());// TODO: replace with a get key in tx
			q.setFilter("(id == line.id) && (language == line.language)");
			q.declareParameters(DialogueLine.class.getName() + " line");
			q.setRange(0, 1);

			@SuppressWarnings("unchecked")
			List<DialogueLine> lines = (List<DialogueLine>) q.execute(line);

			if (!lines.isEmpty()) {//TODO: test once we put everything in a single entity group
				LOG.log(Level.SEVERE, "A dialogue line with the same ID and language already exists.");
				throw new ValidationException("A dialogue line with the same ID and language already exists.");
			}// FIXME

			// TODO:each line should be in a entity group with the user/creator,
			// hard set in game lines can use a admin/cyan user
			if (line.getKey() == null) {//TODO use key
				line.setKey(line.toKey());
			}
			pm.makePersistent(line);
		} finally {
			pm.close();
		}
	}

	// Needed to work around bug (https://code.google.com/p/google-web-toolkit/issues/detail?id=6794)
	@Override
	public void delete(DialogueLine entity) throws ServiceException {
		super.delete(entity);
	}

}

@@ -46,63 +46,10 @@
 o;%0A%0A
-import java.util.List;%0Aimport java.util.logging.Level;%0A
+//
 impo
@@ -126,25 +126,64 @@
 ort 
-javax.jdo.Query;%0A
+com.obductiongame.translate.server.NotLoggedInException;
 %0Aimp
@@ -294,32 +294,94 @@
 ationException;%0A
+import com.obductiongame.translate.server.WrongUserException;%0A
 import com.obduc
@@ -469,14 +469,20 @@
 nds 
-Entity
+UserRequired
 Dao%3C
@@ -499,16 +499,18 @@
 ne%3E %7B%0A%0A%09
+//
 private 
@@ -590,59 +590,8 @@
 ));%0A
-// TODO: use id + lang as key to find entity faster
 %0A%09pu
@@ -717,24 +717,66 @@
 ionException
+, WrongUserException, NotLoggedInException
  %7B%0A%09%09/*if (A
@@ -1147,512 +1147,198 @@
 r();
- 
+%0A%09%09try %7B
 //
- 
 TODO
-: cover to transaction as per google jdo pages%0A%09%09try %7B//https://groups.google.com/forum/#!topic/objectify-appe
+ handle overwriti
 ng
+ 
 in
-e/Jamr_Ib4vlE%0A%09%09%09Query q = pm.newQuery(%22select key from %22 + DialogueLine.class.getName());// TODO: replace with a get key in tx%0A%09%09%09q.setFilter(%22(id == line.id) && (language == line.language)%22);%0A%09%09%09q.declareParameters(DialogueLine.class.getName() + %22 line%22);%0A%09%09%09q.setRange(0, 1);%0A%0A%09%09%09@SuppressWarnings(%22unchecked%22)%0A%09%09%09List%3CDialogueLine%3E lines = (List%3CDialogueLine%3E) q.execute
+ the UI%0A%09%09%09// TODO: add key name/id as entity parameters, make it easier to set name? both here and for user + line entities%0A%09%09%09requireUserAsOwner
 (line);%0A
 %0A%09%09%09
@@ -1337,502 +1337,187 @@
 e);%0A
-%0A
 %09%09%09if (
-!
 line
-s.isEmpty()
+.getKey() == null
 ) %7B
+ 
 //
+ 
 TODO: 
-test once we put everything in a single entity group%0A%09%09%09%09LOG.log(Level.SEVERE, %22A dialogue line with the s
+is there a better way to set n
 ame 
-ID 
 and 
-language already exists.%22);%0A%09%09%09%09throw new ValidationException(%22A dialogue line with the same ID and language already exists.%22);%0A%09%09%09%7D// FIXME%0A%0A%09%09%09// TODO:each line should be in a entity group with the user/creator,%0A%09%09%09// hard set in game lines can use a admin/cyan user%0A%09%09%09if (line.getKey() == null) %7B//TODO use key%0A%09%09%09%09line.setKey(
+not parent?%0A%09%09%09%09line.setKey(getUsersKey().getChild(DialogueLine.class.getSimpleName(), 
 line.to
-Key(
+Name()
 ));%0A

Make dialog name a user entity