/**
 *  CreateGroups
 *  Copyright 22.06.2017 by saurabh
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program in the file lgpl21.txt
 *  If not, see <http://www.gnu.org/licenses/>.
 */

package ai.susi.server.api.cms;

import ai.susi.DAO;
import ai.susi.json.JsonObjectWithDefault;
import ai.susi.server.*;
import org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * This Servlets returns all the group details of all the groups created.
 * It takes one parameter group
 * Can be tested on
 * http://127.0.0.1:4000/cms/createGroups.json?group=groupName
 *
 */
public class CreateGroups extends AbstractAPIHandler implements APIHandler {


    private static final long serialVersionUID = -742269505564698987L;

    @Override
    public BaseUserRole getMinimalBaseUserRole() {
        return BaseUserRole.ADMIN;
    }

    @Override
    public JSONObject getDefaultPermissions(BaseUserRole baseUserRole) {
        return null;
    }

    @Override
    public String getAPIPath() {
        return "/cms/createGroups.json";
    }

    @Override
    public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Authorization rights, final JsonObjectWithDefault permissions) {
        JSONObject result = new JSONObject();
        String group_name = call.get("group", null);
        if( DAO.group.has(group_name)) {
            result.put("success", false);
            result.put("message", "Group already exists");
        } else {
            DAO.group.put(group_name,new JSONObject(),rights.getIdentity().isPersistent());
            result.put("success", true);
            result.put("message", "Group created successfully");
        }
        return new ServiceResponse(result);
    }

}


@@ -1919,16 +1919,53 @@
      if(
+ group_name!=null ) %7B%0A            if(
  DAO.gro
@@ -1982,24 +1982,28 @@
 up_name)) %7B%0A
+    
             
@@ -2036,32 +2036,36 @@
 e);%0A            
+ 
+   
 result.put(%22mess
@@ -2103,16 +2103,20 @@
         
+    
 %7D else %7B
@@ -2128,16 +2128,20 @@
         
+ 
+   
 DAO.grou
@@ -2216,32 +2216,36 @@
 ));%0A            
+    
 result.put(%22succ
@@ -2261,32 +2261,36 @@
 e);%0A            
+ 
+   
 result.put(%22mess
@@ -2322,24 +2322,227 @@
 essfully%22);%0A
+            %7D%0A            return new ServiceResponse(result);%0A        %7D else %7B%0A            result.put(%22success%22,false);%0A            result.put(%22message%22, %22Bad call, group name parameter not specified%22);%0A
         %7D%0A  

check for null,bad url call