package org.sagebionetworks.repo.model.dbo.persistence.discussion;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.sagebionetworks.util.ValidateArgument;

public class DiscussionThreadUtils {
	public static final Charset UTF8 = Charset.forName("UTF-8");

	/**
	 * Create a DBO from user's request
	 * 
	 * @param forumId
	 * @param title
	 * @param messageKey
	 * @param userId
	 * @param id
	 * @param etag
	 * @return
	 */
	public static DBODiscussionThread createDBO(String forumId, String title,
			String messageKey, Long userId, String id, String etag) {
		ValidateArgument.requirement(forumId != null, "forumId");
		ValidateArgument.requirement(title != null, "title");
		ValidateArgument.requirement(messageKey != null, "messageUrl");
		ValidateArgument.requirement(userId != null, "userId");
		ValidateArgument.requirement(id != null, "id");
		ValidateArgument.requirement(etag != null, "etag");
		DBODiscussionThread dbo = new DBODiscussionThread();
		dbo.setId(Long.parseLong(id));
		dbo.setForumId(Long.parseLong(forumId));
		dbo.setTitle(title.getBytes(UTF8));
		dbo.setMessageKey(messageKey);
		dbo.setCreatedBy(userId);
		dbo.setIsEdited(false);
		dbo.setIsDeleted(false);
		dbo.setEtag(etag);
		return dbo;
	}

	/**
	 * convert an input String to a list of String
	 * the input has the following format:
	 * <string1>,<string2>,...
	 * 
	 * @param inputString
	 * @return
	 */
	public static List<String> toList(String inputString) {
		if (inputString == null) {
			throw new IllegalArgumentException("inputString");
		}
		List<String> list = new ArrayList<String>();
		if (inputString.equals("")) {
			return list;
		}
		String[] elements = inputString.split(",");
		for (String string : elements) {
			list.add(string);
		}
		return list;
	}

	/**
	 * convert a list of String to a String with format:
	 * <string1>,<string2>,...
	 * 
	 * @param listOfString
	 * @return
	 */
	public static String toString(List<String> listOfString) {
		String result = "";
		if (listOfString.isEmpty()) {
			return result;
		}
		if (listOfString.size() == 1) {
			return listOfString.get(0);
		}
		result += listOfString.get(0);
		for (int i = 1; i < listOfString.size(); i++) {
			result += ","+listOfString.get(i);
		}
		return result;
	}
}

@@ -633,20 +633,17 @@
 uire
-ment
+d
 (forumId
  != 
@@ -638,24 +638,16 @@
 (forumId
- != null
 , %22forum
@@ -682,26 +682,15 @@
 uire
-ment
+d
 (title
- != null
 , %22t
@@ -723,20 +723,17 @@
 .require
-ment
+d
 (message
@@ -735,24 +735,16 @@
 ssageKey
- != null
 , %22messa
@@ -782,27 +782,16 @@
 uire
-ment
+d
 (userId
- != null
 , %22u
@@ -829,23 +829,12 @@
 uire
-ment(id != null
+d(id
 , %22i
@@ -868,25 +868,14 @@
 uire
-ment
+d
 (etag
- != null
 , %22e

use ValidateArgument.required method to check for null