/*
 *  Copyright (C) 2022 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW 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 Affero General Public License included below for more details.

 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package org.akvo.flow.servlet;

import com.gallatinsystems.framework.rest.AbstractRestApiServlet;
import com.gallatinsystems.framework.rest.RestRequest;
import com.gallatinsystems.framework.rest.RestResponse;
import com.gallatinsystems.survey.dao.SurveyGroupDAO;
import com.gallatinsystems.survey.domain.SurveyGroup;
import com.gallatinsystems.user.dao.UserAuthorizationDAO;
import com.gallatinsystems.user.dao.UserDao;
import com.gallatinsystems.user.dao.UserRoleDao;
import com.gallatinsystems.user.domain.User;
import com.gallatinsystems.user.domain.UserAuthorization;
import com.gallatinsystems.user.domain.UserRole;
import org.akvo.flow.domain.DefaultUserAuthorization;
import org.akvo.flow.rest.dto.PostUserRegistrationRestRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.logging.Logger;

public class PostUserRegistrationServlet extends AbstractRestApiServlet {
    private final String MAIN_DOMAIN = "www";

    private final String DEMO_DOMAIN = "demo";

    private UserDao userDao = new UserDao();

    private SurveyGroupDAO folderDao = new SurveyGroupDAO();

    @Override
    protected RestRequest convertRequest() throws Exception {
        HttpServletRequest req = getRequest();
        RestRequest restRequest = new PostUserRegistrationRestRequest();
        restRequest.populateFromHttpRequest(req);
        return restRequest;

    }

    @Override
    protected RestResponse handleRequest(RestRequest req) throws Exception {
        PostUserRegistrationRestRequest request = (PostUserRegistrationRestRequest) req;
        if (!MAIN_DOMAIN.equalsIgnoreCase(request.getDomain()) || !DEMO_DOMAIN.equalsIgnoreCase(request.getDomain())) {
            return new RestResponse();
        }

        User newUser = addUser(request.getEmail(), request.getFullName());
        SurveyGroup folder = addFolder(newUser.getUserName(), newUser.getKey().getId());
        addAuthorization(newUser, folder);

        return new RestResponse();
    }

    private User addUser(String email, String fullName) {
        User newUser = new User();
        newUser.setEmailAddress(email);
        newUser.setUserName(fullName);

        return userDao.save(newUser);
    }

    private SurveyGroup addFolder(String folderName, Long creationUserId) {
        SurveyGroup folder = new SurveyGroup();
        folder.setProjectType(SurveyGroup.ProjectType.PROJECT_FOLDER);
        folder.setName(folderName);
        folder.setCode(folderName);
        folder.setCreateUserId(creationUserId);

        return folderDao.save(folder);
    }

    private void addAuthorization(User newUser, SurveyGroup folder) {
        UserAuthorization authorization = DefaultUserAuthorization
                .getOrCreateDefaultAuthorization(newUser.getKey().getId(), folder.getKey().getId());
        new UserAuthorizationDAO().save(authorization);
    }

    @Override
    protected void writeOkResponse(RestResponse resp) throws Exception {

    }
}

@@ -2157,32 +2157,80 @@
 ows Exception %7B%0A
+        RestResponse resp = new RestResponse();%0A
         PostUser
@@ -2302,16 +2302,16 @@
 t) req;%0A
-
         
@@ -2436,39 +2436,87 @@
       re
-turn new RestResponse()
+sp.setMessage(%22Not valid registration domain%22);%0A            return resp
 ;%0A      
@@ -2742,31 +2742,68 @@
   re
-turn new RestResponse()
+sp.setMessage(%22Registration completed%22);%0A        return resp
 ;%0A  
@@ -2988,22 +2988,28 @@
         
-return
+User saved =
  userDao
@@ -3019,24 +3019,47 @@
 ve(newUser);
+%0A%0A        return saved;
 %0A    %7D%0A%0A    
@@ -3378,22 +3378,35 @@
         
-return
+SurveyGroup saved =
  folderD
@@ -3421,16 +3421,39 @@
 folder);
+%0A%0A        return saved;
 %0A    %7D%0A%0A

Add text in response