<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.stratelia.webactiv.clipboard.control;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import javax.transaction.Transactional;

import org.silverpeas.search.indexEngine.model.IndexEntry;

import org.silverpeas.util.clipboard.ClipboardException;
import org.silverpeas.util.clipboard.ClipboardSelection;

import com.stratelia.silverpeas.silvertrace.SilverTrace;

import static org.silverpeas.util.clipboard.ClipboardSelection.IndexFlavor;

/**
 * Silverpeas Service to maintain the status of the main clipboard.
 */
@Singleton
@Transactional
@MainClipboard
public class MainClipboardService implements Clipboard, Serializable {

  private static final long serialVersionUID = -824732581358882058L;
  private ClipboardSelection lastObject = null;
  private ArrayList<ClipboardSelection> objectsInClipboard = null;
  private boolean multipleClipboardSupported = true;
  private boolean addingToSelection = true;
  private int count = 0;
  private String name = null;
  /**
   * User alert in case of error during operation
   */
  private String errorMessage = null;
  private Exception error = null;

  /**
   * Copy a node.
   *
   * @param objectToCopy
   * @throws ClipboardException
   */
  @Override
  public void add(ClipboardSelection objectToCopy) throws ClipboardException {
    try {

      count += 1;
      boolean failed = false;
      if (objectToCopy != null) {
        if (!addingToSelection) {
          // we have to deselect the object still in clipboard
          for (ClipboardSelection clipObject : objectsInClipboard) {
            clipObject.setSelected(false);
          }
          // now we add...
          addingToSelection = true;
        }
        if (objectToCopy.isDataFlavorSupported(IndexFlavor)) {
          try {
            IndexEntry MainIndexEntry = (IndexEntry) objectToCopy.getTransferData(IndexFlavor);
            for (ClipboardSelection clipObject : objectsInClipboard) {
              if (clipObject.isDataFlavorSupported(IndexFlavor)) {
                IndexEntry indexEntry = (IndexEntry) clipObject.getTransferData(IndexFlavor);
                if (indexEntry.equals(MainIndexEntry)) {
                  clipObject.setSelected(true);
                  throw new Exception("");
                }
              }
            }
          } catch (Exception e) {
            // We dont add an other copy of this object
            // but we need to select it
            objectToCopy.setSelected(true);
            failed = true;
          }
        }
        if (!failed) {
          lastObject = objectToCopy;
          if (multipleClipboardSupported) {
            objectsInClipboard.add(lastObject);
            lastObject.setSelected(true);
          }
        }
      }

    } catch (Exception e) {
      SilverTrace.warn("clipboard", "ClipboardBmEJB.add()", "root.MSG_GEN_ERROR",
          "ERROR occured in ClipboardBmEJB.add()", e);
      throw new ClipboardException("ClipboardBmEJB", SilverTrace.TRACE_LEVEL_ERROR,
          "ERROR occured in ClipboardBmEJB.add()", e);
    }<commit_msg>fixing bug #7333. Now, last operation is one performed.<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.stratelia.webactiv.clipboard.control;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import javax.transaction.Transactional;

import org.silverpeas.search.indexEngine.model.IndexEntry;

import org.silverpeas.util.clipboard.ClipboardException;
import org.silverpeas.util.clipboard.ClipboardSelection;

import com.stratelia.silverpeas.silvertrace.SilverTrace;

import static org.silverpeas.util.clipboard.ClipboardSelection.IndexFlavor;

/**
 * Silverpeas Service to maintain the status of the main clipboard.
 */
@Singleton
@Transactional
@MainClipboard
public class MainClipboardService implements Clipboard, Serializable {

  private static final long serialVersionUID = -824732581358882058L;
  private ClipboardSelection lastObject = null;
  private ArrayList<ClipboardSelection> objectsInClipboard = null;
  private boolean multipleClipboardSupported = true;
  private boolean addingToSelection = true;
  private int count = 0;
  private String name = null;
  /**
   * User alert in case of error during operation
   */
  private String errorMessage = null;
  private Exception error = null;

  /**
   * Copy a node.
   *
   * @param objectToCopy
   * @throws ClipboardException
   */
  @Override
  public void add(ClipboardSelection objectToCopy) throws ClipboardException {
    try {

      count += 1;
      boolean failed = false;
      if (objectToCopy != null) {
        if (!addingToSelection) {
          // we have to deselect the object still in clipboard
          for (ClipboardSelection clipObject : objectsInClipboard) {
            clipObject.setSelected(false);
          }
          // now we add...
          addingToSelection = true;
        }
        if (objectToCopy.isDataFlavorSupported(IndexFlavor)) {
          try {
            IndexEntry MainIndexEntry = (IndexEntry) objectToCopy.getTransferData(IndexFlavor);
            for (ClipboardSelection clipObject : objectsInClipboard) {
              if (clipObject.isDataFlavorSupported(IndexFlavor)) {
                IndexEntry indexEntry = (IndexEntry) clipObject.getTransferData(IndexFlavor);
                if (indexEntry.equals(MainIndexEntry)) {
                  clipObject.setSelected(true);
                  clipObject.setCutted(objectToCopy.isCutted());
                  throw new Exception("");
                }
              }
            }
          } catch (Exception e) {
            // We dont add an other copy of this object
            // but we need to select it
            objectToCopy.setSelected(true);
            failed = true;
          }
        }
        if (!failed) {
          lastObject = objectToCopy;
          if (multipleClipboardSupported) {
            objectsInClipboard.add(lastObject);
            lastObject.setSelected(true);
          }
        }
      }

    } catch (Exception e) {
      SilverTrace.warn("clipboard", "ClipboardBmEJB.add()", "root.MSG_GEN_ERROR",
          "ERROR occured in ClipboardBmEJB.add()", e);
      throw new ClipboardException("ClipboardBmEJB", SilverTrace.TRACE_LEVEL_ERROR,
          "ERROR occured in ClipboardBmEJB.add()", e);
    }
{'fraction_non_alphanumeric': 0.07985049269452939, 'fraction_numerical': 0.007475365273530411, 'mean_word_length': 2.9667565139263252, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2282629', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2335, 'n_words': 755}