private JSONArray getPublishableLayers(final JSONArray selectedLayers, final User user)
      throws ActionException {
    if (selectedLayers == null || user == null) {
      throw new ActionParamsException("Could not get selected layers");
    }
    final JSONArray filteredList = new JSONArray();
    log.debug("Selected layers:", selectedLayers);

    String userUuid = user.getUuid();
    try {
      for (int i = 0; i < selectedLayers.length(); ++i) {
        JSONObject layer = selectedLayers.getJSONObject(i);
        String layerId = layer.getString("id");
        if (layerId.startsWith(PREFIX_MYPLACES)) {
          // check publish right for published myplaces layer
          if (hasRightToPublishMyPlaceLayer(layerId, userUuid, user.getScreenname())) {
            filteredList.put(layer);
          }
        } else if (layerId.startsWith(PREFIX_BASELAYER)) {
          // check publish right for base layer
          if (hasRightToPublishBaseLayer(layerId, user)) {
            filteredList.put(layer);
          }
        }
        // check publish right for normal layer
        else if (hasRightToPublishLayer(layerId, user)) {
          filteredList.put(layer);
        }
      }
    } catch (Exception e) {
      log.error(e, "Error parsing myplaces layers from published layers", selectedLayers);
    }
    log.debug("Filtered layers:", filteredList);
    return filteredList;
  }
  @Override
  public void handlePost(ActionParameters params) throws ActionException {
    log.debug("handlePost");
    User user = new User();
    getUserParams(user, params);
    String[] roles = params.getRequest().getParameterValues("roles");
    String password = params.getHttpParam(PARAM_PASSWORD);
    User retUser = null;
    try {
      if (user.getId() > -1) {
        // retUser = userService.modifyUser(user);
        log.debug("roles size: " + roles.length);
        retUser = userService.modifyUserwithRoles(user, roles);
        log.debug("done modifying user");
        if (password != null && !"".equals(password.trim())) {
          userService.updateUserPassword(retUser.getScreenname(), password);
        }
      } else {
        log.debug("NOW IN POST and creating a new user!!!!!!!!!!!!!");
        if (password == null || password.trim().isEmpty()) {
          throw new ActionException("Parameter 'password' not found.");
        }
        retUser = userService.createUser(user);
        userService.setUserPassword(retUser.getScreenname(), password);
      }

    } catch (ServiceException se) {
      throw new ActionException(se.getMessage(), se);
    }
    JSONObject response = null;
    try {
      response = user2Json(retUser);
    } catch (JSONException je) {
      throw new ActionException(je.getMessage(), je);
    }
    ResponseHelper.writeResponse(params, response);
  }
  private JSONObject user2Json(User user) throws JSONException {
    // TODO: User.toJSON() should be used
    JSONObject uo = new JSONObject();
    uo.put("id", user.getId());
    uo.put("firstName", user.getFirstname());
    uo.put("lastName", user.getLastname());
    uo.put("user", user.getScreenname());
    uo.put("email", user.getEmail());

    JSONArray rolesArray = new JSONArray();
    for (Role role : user.getRoles()) {
      rolesArray.put(role.getId());
    }
    JSONHelper.put(uo, "roles", rolesArray);

    return uo;
  }
 @Override
 public void handlePut(ActionParameters params) throws ActionException {
   log.debug("handlePut");
   User user = new User();
   getUserParams(user, params);
   String password = params.getRequiredParam(PARAM_PASSWORD);
   String[] roles = params.getRequest().getParameterValues("roles");
   User retUser = null;
   try {
     retUser = userService.createUser(user, roles);
     userService.setUserPassword(retUser.getScreenname(), password);
   } catch (ServiceException se) {
     throw new ActionException(se.getMessage(), se);
   }
   JSONObject response = null;
   try {
     response = user2Json(retUser);
   } catch (JSONException je) {
     throw new ActionException(je.getMessage(), je);
   }
   ResponseHelper.writeResponse(params, response);
 }
  public void handleAction(ActionParameters params) throws ActionException {

    final User user = params.getUser();

    // Parse stuff sent by JS
    final JSONObject publisherData = getPublisherInput(params.getRequiredParam(KEY_PUBDATA));
    final View currentView = getBaseView(publisherData, user);

    final Bundle mapFullBundle = currentView.getBundleByName(ViewModifier.BUNDLE_MAPFULL);
    if (mapFullBundle == null) {
      throw new ActionParamsException("Could find mapfull bundle from view:" + currentView.getId());
    }

    // Setup user
    try {
      JSONObject userJson = new JSONObject();
      userJson.put(KEY_FIRSTNAME, user.getFirstname());
      userJson.put(KEY_LASTNAME, user.getLastname());
      userJson.put(KEY_NICKNAME, user.getScreenname());
      userJson.put(KEY_LOGINNAME, user.getEmail());
      JSONHelper.putValue(mapFullBundle.getConfigJSON(), KEY_USER, userJson);
      // mapfullTemplateConfig.put(KEY_USER, userJson);
    } catch (JSONException jsonex) {
      log.error("Could not create user object:", user, "- Error:", jsonex.getMessage());
      throw new ActionParamsException("User data problem");
    }

    // setup basic info about view
    final String domain = JSONHelper.getStringFromJSON(publisherData, KEY_DOMAIN, null);
    if (domain == null) {
      throw new ActionParamsException("Domain missing");
    }
    final String name =
        JSONHelper.getStringFromJSON(
            publisherData, KEY_NAME, "Published map " + System.currentTimeMillis());
    final String language =
        JSONHelper.getStringFromJSON(
            publisherData, KEY_LANGUAGE, PropertyUtil.getDefaultLanguage());

    currentView.setPubDomain(domain);
    currentView.setName(name);
    currentView.setType(params.getHttpParam(ViewTypes.VIEW_TYPE, ViewTypes.PUBLISHED));
    currentView.setCreator(user.getId());
    currentView.setIsPublic(true);
    // application/page/developmentPath should be configured to publish template view
    currentView.setLang(language);

    // setup map state
    setupMapState(mapFullBundle, publisherData, user);

    // setup infobox
    final JSONObject tmpInfoboxState = publisherData.optJSONObject(ViewModifier.BUNDLE_INFOBOX);
    if (tmpInfoboxState != null) {
      final Bundle infoboxTemplateBundle = currentView.getBundleByName(ViewModifier.BUNDLE_INFOBOX);
      if (infoboxTemplateBundle != null) {
        infoboxTemplateBundle.setState(tmpInfoboxState.toString());
      } else {
        log.warn(
            "Publisher sent state for infobox, but infobox isn't available in template view! State:",
            tmpInfoboxState);
      }
    }

    // Setup publishedmyplaces2 bundle if user has configured it/has permission to do so
    if (user.hasAnyRoleIn(drawToolsEnabledRoles)) {
      setupBundle(currentView, publisherData, ViewModifier.BUNDLE_PUBLISHEDMYPLACES2);
    }

    // Setup toolbar bundle if user has configured it
    setupBundle(currentView, publisherData, ViewModifier.BUNDLE_TOOLBAR);

    // Setup thematic map/published grid bundle
    final JSONObject gridState = publisherData.optJSONObject(KEY_GRIDSTATE);
    log.debug("Grid state:", gridState);
    if (gridState != null) {
      final Bundle gridBundle = addBundle(currentView, ViewModifier.BUNDLE_PUBLISHEDGRID);
      log.debug("Grid bundle added:", gridBundle);
      mergeBundleConfiguration(gridBundle, null, gridState);
    }

    log.debug("Save view:", currentView);
    final View newView = saveView(currentView);
    log.debug("Published a map:", newView);

    try {
      JSONObject newViewJson = new JSONObject(newView.toString());
      ResponseHelper.writeResponse(params, newViewJson);
    } catch (JSONException je) {
      log.error(je, "Could not create JSON response.");
      ResponseHelper.writeResponse(params, false);
    }
  }