<commit_before>/**
 * Copyright (C) 2000 - 2013 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * This program 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 for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.silverpeas.domains.silverpeasdriver;

import com.stratelia.webactiv.beans.admin.AbstractDomainDriver;
import com.stratelia.webactiv.beans.admin.AdminException;
import com.stratelia.webactiv.beans.admin.DomainProperty;
import com.stratelia.webactiv.beans.admin.Group;
import com.stratelia.webactiv.beans.admin.UserDetail;
import com.stratelia.webactiv.beans.admin.UserFull;
import org.silverpeas.authentication.encryption.PasswordEncryption;
import org.silverpeas.authentication.encryption.PasswordEncryptionProvider;
import org.silverpeas.util.DBUtil;
import org.silverpeas.util.ResourceLocator;
import org.silverpeas.util.StringUtil;
import org.silverpeas.util.exception.SilverpeasException;
import org.silverpeas.util.exception.UtilException;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.transaction.Transactional;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

@Singleton
@Transactional
public class SilverpeasDriver extends AbstractDomainDriver implements SilverpeasDomainDriver {

  @Inject
  private SPUserManager userManager;

  @Inject
  private SPGroupManager groupManager;

  /**
   * Constructor
   */
  public SilverpeasDriver() {
  }

  /**
   * Virtual method that performs extra initialization from a properties file. To overload by the
   * class who need it.
   * @throws Exception
   */
  @Override
  public void initFromProperties(ResourceLocator rs) throws Exception {
  }

  @Override
  public UserDetail[] getAllChangedUsers(String fromTimeStamp, String toTimeStamp)
      throws Exception {
    return new UserDetail[0];
  }

  @Override
  public Group[] getAllChangedGroups(String fromTimeStamp, String toTimeStamp) throws Exception {
    return new Group[0];
  }

  @Override
  public UserDetail importUser(String userLogin) throws Exception {
    return null;
  }

  @Override
  public void removeUser(String userId) throws Exception {
  }

  @Override
  public UserDetail synchroUser(String userId) throws Exception {
    return null;
  }

  /**
   * @param ud a user detail
   * @return the new user id.
   */
  @Override
  public String createUser(UserDetail ud) {
    try {
      SPUser user = convertToSPUser(ud, new SPUser());
      int id = DBUtil.getNextId("domainsp_user", "id");
      user.setId(id);
      user = userManager.saveAndFlush(user);
      return String.valueOf(id);
    } catch (SQLException ex) {
      Logger.getLogger(SilverpeasDriver.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "-1";
  }

  /**
   * @param userId
   */
  @Override
  public void deleteUser(String userId) {
    if (StringUtil.isInteger(userId)) {
      SPUser user = userManager.getById(userId);
      if (user.getGroups() != null) {
        for (SPGroup group : user.getGroups()) {
          group.getUsers().remove(user);
          groupManager.saveAndFlush(group);
        }
      }
      userManager.delete(user);
    }
  }

  @Override
  public void updateUserFull(UserFull userFull) throws UtilException {
    SPUser oldUser = userManager.getById(userFull.getSpecificId());
    oldUser.setFirstname(userFull.getFirstName());
    oldUser.setLastname(userFull.getLastName());
    oldUser.setLogin(userFull.getLogin());
    oldUser.setEmail(userFull.geteMail());

    oldUser.setTitle(userFull.getValue("title"));<commit_msg>change Exception from SQLException to JPA integrated exception<commit_after>/**
 * Copyright (C) 2000 - 2013 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * This program 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 for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.silverpeas.domains.silverpeasdriver;

import com.stratelia.webactiv.beans.admin.AbstractDomainDriver;
import com.stratelia.webactiv.beans.admin.AdminException;
import com.stratelia.webactiv.beans.admin.DomainProperty;
import com.stratelia.webactiv.beans.admin.Group;
import com.stratelia.webactiv.beans.admin.UserDetail;
import com.stratelia.webactiv.beans.admin.UserFull;
import org.silverpeas.authentication.encryption.PasswordEncryption;
import org.silverpeas.authentication.encryption.PasswordEncryptionProvider;
import org.silverpeas.util.DBUtil;
import org.silverpeas.util.ResourceLocator;
import org.silverpeas.util.StringUtil;
import org.silverpeas.util.exception.SilverpeasException;
import org.silverpeas.util.exception.UtilException;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.transaction.Transactional;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

@Singleton
@Transactional
public class SilverpeasDriver extends AbstractDomainDriver implements SilverpeasDomainDriver {

  @Inject
  private SPUserManager userManager;

  @Inject
  private SPGroupManager groupManager;

  /**
   * Constructor
   */
  public SilverpeasDriver() {
  }

  /**
   * Virtual method that performs extra initialization from a properties file. To overload by the
   * class who need it.
   * @throws Exception
   */
  @Override
  public void initFromProperties(ResourceLocator rs) throws Exception {
  }

  @Override
  public UserDetail[] getAllChangedUsers(String fromTimeStamp, String toTimeStamp)
      throws Exception {
    return new UserDetail[0];
  }

  @Override
  public Group[] getAllChangedGroups(String fromTimeStamp, String toTimeStamp) throws Exception {
    return new Group[0];
  }

  @Override
  public UserDetail importUser(String userLogin) throws Exception {
    return null;
  }

  @Override
  public void removeUser(String userId) throws Exception {
  }

  @Override
  public UserDetail synchroUser(String userId) throws Exception {
    return null;
  }

  /**
   * @param ud a user detail
   * @return the new user id.
   */
  @Override
  public String createUser(UserDetail ud) {
    try {
      SPUser user = convertToSPUser(ud, new SPUser());
      int id = DBUtil.getNextId("domainsp_user", "id");
      user.setId(id);
      user = userManager.saveAndFlush(user);
      return String.valueOf(id);
    } catch (Exception ex) {
      Logger.getLogger(SilverpeasDriver.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "-1";
  }

  /**
   * @param userId
   */
  @Override
  public void deleteUser(String userId) {
    if (StringUtil.isInteger(userId)) {
      SPUser user = userManager.getById(userId);
      if (user.getGroups() != null) {
        for (SPGroup group : user.getGroups()) {
          group.getUsers().remove(user);
          groupManager.saveAndFlush(group);
        }
      }
      userManager.delete(user);
    }
  }

  @Override
  public void updateUserFull(UserFull userFull) throws UtilException {
    SPUser oldUser = userManager.getById(userFull.getSpecificId());
    oldUser.setFirstname(userFull.getFirstName());
    oldUser.setLastname(userFull.getLastName());
    oldUser.setLogin(userFull.getLogin());
    oldUser.setEmail(userFull.geteMail());

    oldUser.setTitle(userFull.getValue("title"));
{'fraction_non_alphanumeric': 0.09261630388390951, 'fraction_numerical': 0.002987622705932565, 'mean_word_length': 4.044671689989236, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1093187', 'n_tokens_mistral': 2761, 'n_tokens_neox': 2712, 'n_words': 778}