package modules.admin.Communication.actions;

import modules.admin.Communication.CommunicationUtil;
import modules.admin.Tag.TagBizlet;
import modules.admin.domain.Communication;
import modules.admin.domain.Communication.ActionType;

import org.skyve.CORE;
import org.skyve.domain.messages.Message;
import org.skyve.domain.messages.ValidationException;
import org.skyve.metadata.controller.ServerSideAction;
import org.skyve.metadata.controller.ServerSideActionResult;
import org.skyve.persistence.Persistence;
import org.skyve.web.WebContext;

public class GetResults implements ServerSideAction<Communication> {
	/**
	 * For Serialization
	 */
	private static final long serialVersionUID = 2886341074753936987L;

	/**
	 * Kick off the annual returns job.
	 */
	@Override
	public ServerSideActionResult execute(Communication communication, WebContext webContext) throws Exception {

		communication.setActionType(ActionType.testBindingsAndOutput);
	
		Persistence pers = CORE.getPersistence();
		
		Communication result = pers.save(communication);
		
		String results = getResults(result);
		
		result.setResults(results);
		
		result = CommunicationUtil.kickOffJob(result);
		
		return new ServerSideActionResult(result);
	}
	
	public static String getResults(Communication bean) throws Exception{
		
		if (bean.getTag() == null) {
			throw new ValidationException(new Message(Communication.tagPropertyName, "A tag must be selected for results."));
		}

		Persistence pers = CORE.getPersistence();
		Communication communication = pers.save(bean);
		
		Long count = TagBizlet.getTaggedCountForDocument(communication.getTag(), communication.getModuleName(), communication.getDocumentName());

		StringBuilder results = new StringBuilder();
		results.append(count).append(" communications for ");
		results.append(communication.getDocumentName());
		results.append(" will be ");
		
		switch (communication.getActionType()){
		case saveForBulkSend:
			results.append("created into the directory ");
			results.append(communication.getFilePath());
			results.append(".");
			break;
			
		case sendImmediately:
			results.append("sent immediately.");
			break;
			
		case testBindingsAndOutput:
			results.append("tested.");
			break;
			
		default:
			break;
		}
		
		System.out.println("RESULTS " + results.toString());
		
		return results.toString();
		
	}
}

@@ -942,17 +942,16 @@
 utput);%0A
-%09
 %0A%09%09Persi
@@ -987,18 +987,16 @@
 ence();%0A
-%09%09
 %0A%09%09Commu
@@ -1039,18 +1039,16 @@
 ation);%0A
-%09%09
 %0A%09%09Strin
@@ -1075,26 +1075,24 @@
 ts(result);%0A
-%09%09
 %0A%09%09result.se
@@ -1110,18 +1110,16 @@
 sults);%0A
-%09%09
 %0A%09%09resul
@@ -1160,18 +1160,16 @@
 esult);%0A
-%09%09
 %0A%09%09retur
@@ -1209,17 +1209,16 @@
 lt);%0A%09%7D%0A
-%09
 %0A%09public
@@ -1279,20 +1279,19 @@
 xception
+ 
 %7B%0A
-%09%09
 %0A%09%09if (b
@@ -1529,18 +1529,16 @@
 (bean);%0A
-%09%09
 %0A%09%09Long 
@@ -1825,16 +1825,65 @@
 ame());%0A
+%0A%09%09if (communication.getActionType() != null) %7B%0A%09
 %09%09result
@@ -1907,18 +1907,16 @@
 e %22);%0A%09%09
-%0A%09
 %09switch 
@@ -1946,18 +1946,20 @@
 nType())
+ 
 %7B%0A
+%09
 %09%09case s
@@ -1974,16 +1974,17 @@
 lkSend:%0A
+%09
 %09%09%09resul
@@ -2025,16 +2025,17 @@
 ory %22);%0A
+%09
 %09%09%09resul
@@ -2073,24 +2073,25 @@
 Path());%0A%09%09%09
+%09
 results.appe
@@ -2098,35 +2098,34 @@
 nd(%22.%22);%0A%09%09%09
+%09
 break;%0A
-%09%09%09
 %0A
+%09
 %09%09case sendI
@@ -2136,16 +2136,17 @@
 iately:%0A
+%09
 %09%09%09resul
@@ -2180,27 +2180,26 @@
 .%22);%0A%09%09%09
+%09
 break;%0A
-%09%09%09
 %0A
+%09
 %09%09case t
@@ -2219,24 +2219,25 @@
 dOutput:%0A%09%09%09
+%09
 results.appe
@@ -2258,20 +2258,19 @@
 %0A%09%09%09
+%09
 break;%0A
-%09
+%0A
 %09%09
-%0A%09
 %09def
@@ -2275,16 +2275,17 @@
 efault:%0A
+%09
 %09%09%09break
@@ -2292,70 +2292,15 @@
 ;%0A%09%09
+%09
 %7D%0A%09%09
+%7D
 %0A
-%09%09System.out.println(%22RESULTS %22 + results.toString());%0A%09%09
 %0A%09%09r
@@ -2325,16 +2325,14 @@
 ring();%0A
-%09%09
 %0A%09%7D%0A%7D%0A

Fix NPE when no ActionTag selected