<commit_before>/*
 * Copyright 2003 - 2009 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

package org.efaps.esjp.earchive.node;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.efaps.admin.datamodel.Type;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Return;
import org.efaps.admin.event.Parameter.ParameterValues;
import org.efaps.admin.event.Return.ReturnValues;
import org.efaps.admin.program.esjp.EFapsRevision;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.db.Checkin;
import org.efaps.db.Checkout;
import org.efaps.db.Context;
import org.efaps.db.Instance;
import org.efaps.db.SearchQuery;
import org.efaps.esjp.earchive.NamesInterface;
import org.efaps.esjp.earchive.repository.Repository;
import org.efaps.util.EFapsException;

/**
 * TODO comment!
 *
 * @author jmox
 * @version $Id$
 */
@EFapsUUID("d6cb382d-a8e8-4b35-9d28-6b344c53f676")
@EFapsRevision("$Rev$")
public class NodeUI implements NamesInterface {

  public Return getTableUI(final Parameter _parameter) throws EFapsException {
    final Return ret = new Return();
    final Instance instance = _parameter.getInstance();
    Node node = null;
    if ("eArchive_Repository".equals(instance.getType().getName())) {
      node = Node.getRootNodeFromDB(new Repository(instance));
    }
    final String parentInstanceKey;
    final long parentId;
    String revision = null;
    if (node == null) {
      parentId = instance.getId();
      final String keyTmp = instance.getKey();
      if (keyTmp.contains(SEPERATOR_REVISION)) {
        final int pos = keyTmp.indexOf(SEPERATOR_REVISION);
        revision = keyTmp.substring(pos);
        parentInstanceKey = keyTmp.substring(0, pos);
      } else {
        parentInstanceKey = keyTmp;
      }
    } else {
      parentId = node.getId();
      parentInstanceKey = node.getHistoryId() + SEPERATOR_IDS + node.getCopyId();
    }
    final SearchQuery query = new SearchQuery();
    query.setQueryTypes("eArchive_Node2NodeView");
    query.setExpandChildTypes(true);
    query.addWhereExprEqValue("Parent", parentId);
    query.addSelect("NodeType");
    query.addSelect("Child");
    query.addSelect("HistoryId");
    query.addSelect("CopyId");
    query.execute();

    final List<List<Instance>> list = new ArrayList<List<Instance>>();
    while (query.next()) {
      final List<Instance> instances = new ArrayList<Instance>(1);
      final StringBuilder instanceKey = new StringBuilder()
        .append(parentInstanceKey).append(SEPERATOR_INSTANCE)
        .append(query.get("HistoryId")).append(SEPERATOR_IDS).append(query.get("CopyId"))
        .append(revision != null ? revision : "");

      Type type = Type.get((Long) query.get("NodeType"));
      if (revision != null) {
        if (type.getName().equals(TYPE_NODEDIRECTORY)) {
          type = Type.get(TYPE_NODEDIRECTORYREV);
        } else {
          type = Type.get(TYPE_NODEFILEREV);
        }
      }
      instances.add(Instance.get(type ,
                                (Long) query.get("Child"),
                                instanceKey.toString()));
      list.add(instances);
    }

    ret.put(ReturnValues.VALUES, list);

    return ret;
  }


  public Return getRevisionTableUI(final Parameter _parameter)
      throws EFapsException {
    final Return ret = new Return();
    final Instance instance = _parameter.getInstance();
    final Node node;
    final String instanceKey;
    if ("eArchive_Repository".equals(instance.getType().getName())) {
      node = Node.getRootNodeFromDB(new Repository(instance));
      instanceKey = node.getHistoryId() + SEPERATOR_INSTANCE + node.getCopyId();
    } else {
      node = Node.getNodeFromDB(instance.getId(), instance.getKey());
      instanceKey = instance.getKey();
    }

    final SearchQuery query = new SearchQuery();
    query.setQueryTypes(TYPE_NODEABSTRACTREV);
    query.setExpandChildTypes(true);
    query.addWhereExprEqValue(TYPE_NODEABSTRACTREV_A_HISTORYID,
                              node.getHistoryId());
    query.addWhereExprEqValue(TYPE_NODEABSTRACTREV_A_COPYID, node.getCopyId());
    query.addSelect(TYPE_NODEABSTRACTREV_A_TYPE);
    query.addSelect(TYPE_NODEABSTRACTREV_A_REVISION);
    query.addSelect(TYPE_NODEABSTRACTREV_A_ID);
    query.execute();

    final List<List<Instance>> list = new ArrayList<List<Instance>>();
    while (query.next()) {
      final List<Instance> instances = new ArrayList<Instance>(1);
      final StringBuilder keyBldr = new StringBuilder()
        .append(instanceKey).append(SEPERATOR_REVISION)
        .append(query.get(TYPE_NODEABSTRACTREV_A_REVISION));

      instances.add(Instance.get((Type) query.get(TYPE_NODEABSTRACTREV_A_TYPE),
                                 (Long) query.get(TYPE_NODEABSTRACTREV_A_ID),
                                 keyBldr.toString()));
      list.add(instances);
    }

    ret.put(ReturnValues.VALUES, list);

    return ret;
  }


  public Return createDirectory(final Parameter _parameter)
      throws EFapsException {
    final String name = _parameter.getParameterValue("name");
    final Instance instance = _parameter.getInstance();
    final Node parentNode;
    if ("eArchive_Repository".equals(instance.getType().getName())) {
      parentNode = Node.getRootNodeFromDB(new Repository(instance));
    } else {
      parentNode = Node.getNodeFromDB(instance.getId(), instance.getKey());
    }
    final Node newDir = Node.createNewNode(name, Node.TYPE_NODEDIRECTORY);
    newDir.connect2Parent(parentNode);
    return new Return();
  }

  public Return createFile(final Parameter _parameter)
      throws EFapsException {
    final String name = _parameter.getParameterValue("name");
    final Instance instance = _parameter.getInstance();
    final Node node;
    if ("eArchive_Repository".equals(instance.getType().getName())) {
      node = Node.getRootNodeFromDB(new Repository(instance));
    } else {
      node = Node.getNodeFromDB(instance.getId(), instance.getKey());
    }
    final Node newFile = Node.createNewNode(name, Node.TYPE_NODEFILE);
    newFile.connect2Parent(node);
    final Instance fileInstance = Instance.get(Type.get(TYPE_FILE),
                                               newFile.getFileId());
    final Context.FileParameter fileItem =
                Context.getThreadContext().getFileParameters().get("upload");

    final Checkin checkin = new Checkin(fileInstance);
    try {
      checkin.execute(fileItem.getName(), fileItem.getInputStream(),
          (int) fileItem.getSize());
    } catch (final IOException e) {
      throw new EFapsException(this.getClass(), "execute", e, _parameter);
    }

    return  new Return();
  }


  public Return rename(final Parameter _parameter)
    throws EFapsException {
    final String name = _parameter.getParameterValue("name");
    final Instance instance = _parameter.getInstance();

    final Node node = Node.getNodeFromDB(instance.getId(), instance.getKey());
    node.rename(name);
    return new Return();
  }


  public Return getInstance(final Parameter _parameter) throws EFapsException {
    final String instanceKey = (String) _parameter.get(ParameterValues.OTHERS);
    Instance instance = null;
    if (instanceKey != null) {
      final boolean revision = instanceKey.contains(SEPERATOR_REVISION);
      if (instanceKey.indexOf(SEPERATOR_INSTANCE) < 0 && !revision) {
        instance = Instance.get(instanceKey);
      } else {
        final List<Node> nodes = Node.getNodeHirachy(instanceKey);
        final Node node = nodes.get(nodes.size() - 1);
        Type type = node.getType();
        if (revision) {
          if (type.getName().equals(TYPE_NODEDIRECTORY)) {
            type = Type.get(TYPE_NODEDIRECTORYREV);
          } else {
            type = Type.get(TYPE_NODEFILEREV);
          }
        }
        instance = Instance.get(type, node.getId(), instanceKey);
      }
    }
    final Return ret = new Return();
    ret.put(ReturnValues.VALUES, instance);
    return ret;
  }

  public Return removeNode(final Parameter _parameter) throws EFapsException {
    final Instance instance = _parameter.getInstance();
    final String[] instanceKeys
                            = (String[]) _parameter.get(ParameterValues.OTHERS);
    if (instanceKeys != null) {
      final Node node;
      if ("eArchive_Repository".equals(instance.getType().getName())) {
        node = Node.getRootNodeFromDB(new Repository(instance));
      } else {
        node = Node.getNodeFromDB(instance.getId(), instance.getKey());
      }
      node.deleteChildren(instanceKeys);
    }
    return  new Return();
  }

  public Return checkout(final Parameter _parameter) throws EFapsException {
    final Return ret = new Return();
    final Instance instance = _parameter.getInstance();
    final Node node = Node.getNodeFromDB(instance.getId(), instance.getKey());

    final Checkout checkout = new Checkout(Instance.get(Type.get(TYPE_FILE),
                                                           node.getFileId()));
    File file = null;
    try {
      checkout.preprocess();
      file = File.createTempFile(checkout.getFileName(), ".txt");

      final FileOutputStream stream = new FileOutputStream(file);
      checkout.execute(stream);
    } catch (final Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    ret.put(ReturnValues.VALUES, file);
    return ret;
  }
}
<commit_msg>- BugFix: wrong seperator was used<commit_after>/*
 * Copyright 2003 - 2009 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

package org.efaps.esjp.earchive.node;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.efaps.admin.datamodel.Type;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Return;
import org.efaps.admin.event.Parameter.ParameterValues;
import org.efaps.admin.event.Return.ReturnValues;
import org.efaps.admin.program.esjp.EFapsRevision;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.db.Checkin;
import org.efaps.db.Checkout;
import org.efaps.db.Context;
import org.efaps.db.Instance;
import org.efaps.db.SearchQuery;
import org.efaps.esjp.earchive.NamesInterface;
import org.efaps.esjp.earchive.repository.Repository;
import org.efaps.util.EFapsException;

/**
 * TODO comment!
 *
 * @author jmox
 * @version $Id$
 */
@EFapsUUID("d6cb382d-a8e8-4b35-9d28-6b344c53f676")
@EFapsRevision("$Rev$")
public class NodeUI implements NamesInterface {

  public Return getTableUI(final Parameter _parameter) throws EFapsException {
    final Return ret = new Return();
    final Instance instance = _parameter.getInstance();
    Node node = null;
    if ("eArchive_Repository".equals(instance.getType().getName())) {
      node = Node.getRootNodeFromDB(new Repository(instance));
    }
    final String parentInstanceKey;
    final long parentId;
    String revision = null;
    if (node == null) {
      parentId = instance.getId();
      final String keyTmp = instance.getKey();
      if (keyTmp.contains(SEPERATOR_REVISION)) {
        final int pos = keyTmp.indexOf(SEPERATOR_REVISION);
        revision = keyTmp.substring(pos);
        parentInstanceKey = keyTmp.substring(0, pos);
      } else {
        parentInstanceKey = keyTmp;
      }
    } else {
      parentId = node.getId();
      parentInstanceKey = node.getHistoryId() + SEPERATOR_IDS + node.getCopyId();
    }
    final SearchQuery query = new SearchQuery();
    query.setQueryTypes("eArchive_Node2NodeView");
    query.setExpandChildTypes(true);
    query.addWhereExprEqValue("Parent", parentId);
    query.addSelect("NodeType");
    query.addSelect("Child");
    query.addSelect("HistoryId");
    query.addSelect("CopyId");
    query.execute();

    final List<List<Instance>> list = new ArrayList<List<Instance>>();
    while (query.next()) {
      final List<Instance> instances = new ArrayList<Instance>(1);
      final StringBuilder instanceKey = new StringBuilder()
        .append(parentInstanceKey).append(SEPERATOR_INSTANCE)
        .append(query.get("HistoryId")).append(SEPERATOR_IDS).append(query.get("CopyId"))
        .append(revision != null ? revision : "");

      Type type = Type.get((Long) query.get("NodeType"));
      if (revision != null) {
        if (type.getName().equals(TYPE_NODEDIRECTORY)) {
          type = Type.get(TYPE_NODEDIRECTORYREV);
        } else {
          type = Type.get(TYPE_NODEFILEREV);
        }
      }
      instances.add(Instance.get(type ,
                                (Long) query.get("Child"),
                                instanceKey.toString()));
      list.add(instances);
    }

    ret.put(ReturnValues.VALUES, list);

    return ret;
  }


  public Return getRevisionTableUI(final Parameter _parameter)
      throws EFapsException {
    final Return ret = new Return();
    final Instance instance = _parameter.getInstance();
    final Node node;
    final String instanceKey;
    if ("eArchive_Repository".equals(instance.getType().getName())) {
      node = Node.getRootNodeFromDB(new Repository(instance));
      instanceKey = node.getHistoryId() + SEPERATOR_IDS + node.getCopyId();
    } else {
      node = Node.getNodeFromDB(instance.getId(), instance.getKey());
      instanceKey = instance.getKey();
    }

    final SearchQuery query = new SearchQuery();
    query.setQueryTypes(TYPE_NODEABSTRACTREV);
    query.setExpandChildTypes(true);
    query.addWhereExprEqValue(TYPE_NODEABSTRACTREV_A_HISTORYID,
                              node.getHistoryId());
    query.addWhereExprEqValue(TYPE_NODEABSTRACTREV_A_COPYID, node.getCopyId());
    query.addSelect(TYPE_NODEABSTRACTREV_A_TYPE);
    query.addSelect(TYPE_NODEABSTRACTREV_A_REVISION);
    query.addSelect(TYPE_NODEABSTRACTREV_A_ID);
    query.execute();

    final List<List<Instance>> list = new ArrayList<List<Instance>>();
    while (query.next()) {
      final List<Instance> instances = new ArrayList<Instance>(1);
      final StringBuilder keyBldr = new StringBuilder()
        .append(instanceKey).append(SEPERATOR_REVISION)
        .append(query.get(TYPE_NODEABSTRACTREV_A_REVISION));

      instances.add(Instance.get((Type) query.get(TYPE_NODEABSTRACTREV_A_TYPE),
                                 (Long) query.get(TYPE_NODEABSTRACTREV_A_ID),
                                 keyBldr.toString()));
      list.add(instances);
    }

    ret.put(ReturnValues.VALUES, list);

    return ret;
  }


  public Return createDirectory(final Parameter _parameter)
      throws EFapsException {
    final String name = _parameter.getParameterValue("name");
    final Instance instance = _parameter.getInstance();
    final Node parentNode;
    if ("eArchive_Repository".equals(instance.getType().getName())) {
      parentNode = Node.getRootNodeFromDB(new Repository(instance));
    } else {
      parentNode = Node.getNodeFromDB(instance.getId(), instance.getKey());
    }
    final Node newDir = Node.createNewNode(name, Node.TYPE_NODEDIRECTORY);
    newDir.connect2Parent(parentNode);
    return new Return();
  }

  public Return createFile(final Parameter _parameter)
      throws EFapsException {
    final String name = _parameter.getParameterValue("name");
    final Instance instance = _parameter.getInstance();
    final Node node;
    if ("eArchive_Repository".equals(instance.getType().getName())) {
      node = Node.getRootNodeFromDB(new Repository(instance));
    } else {
      node = Node.getNodeFromDB(instance.getId(), instance.getKey());
    }
    final Node newFile = Node.createNewNode(name, Node.TYPE_NODEFILE);
    newFile.connect2Parent(node);
    final Instance fileInstance = Instance.get(Type.get(TYPE_FILE),
                                               newFile.getFileId());
    final Context.FileParameter fileItem =
                Context.getThreadContext().getFileParameters().get("upload");

    final Checkin checkin = new Checkin(fileInstance);
    try {
      checkin.execute(fileItem.getName(), fileItem.getInputStream(),
          (int) fileItem.getSize());
    } catch (final IOException e) {
      throw new EFapsException(this.getClass(), "execute", e, _parameter);
    }

    return  new Return();
  }


  public Return rename(final Parameter _parameter)
    throws EFapsException {
    final String name = _parameter.getParameterValue("name");
    final Instance instance = _parameter.getInstance();

    final Node node = Node.getNodeFromDB(instance.getId(), instance.getKey());
    node.rename(name);
    return new Return();
  }


  public Return getInstance(final Parameter _parameter) throws EFapsException {
    final String instanceKey = (String) _parameter.get(ParameterValues.OTHERS);
    Instance instance = null;
    if (instanceKey != null) {
      final boolean revision = instanceKey.contains(SEPERATOR_REVISION);
      if (instanceKey.indexOf(SEPERATOR_INSTANCE) < 0 && !revision) {
        instance = Instance.get(instanceKey);
      } else {
        final List<Node> nodes = Node.getNodeHirachy(instanceKey);
        final Node node = nodes.get(nodes.size() - 1);
        Type type = node.getType();
        if (revision) {
          if (type.getName().equals(TYPE_NODEDIRECTORY)) {
            type = Type.get(TYPE_NODEDIRECTORYREV);
          } else {
            type = Type.get(TYPE_NODEFILEREV);
          }
        }
        instance = Instance.get(type, node.getId(), instanceKey);
      }
    }
    final Return ret = new Return();
    ret.put(ReturnValues.VALUES, instance);
    return ret;
  }

  public Return removeNode(final Parameter _parameter) throws EFapsException {
    final Instance instance = _parameter.getInstance();
    final String[] instanceKeys
                            = (String[]) _parameter.get(ParameterValues.OTHERS);
    if (instanceKeys != null) {
      final Node node;
      if ("eArchive_Repository".equals(instance.getType().getName())) {
        node = Node.getRootNodeFromDB(new Repository(instance));
      } else {
        node = Node.getNodeFromDB(instance.getId(), instance.getKey());
      }
      node.deleteChildren(instanceKeys);
    }
    return  new Return();
  }

  public Return checkout(final Parameter _parameter) throws EFapsException {
    final Return ret = new Return();
    final Instance instance = _parameter.getInstance();
    final Node node = Node.getNodeFromDB(instance.getId(), instance.getKey());

    final Checkout checkout = new Checkout(Instance.get(Type.get(TYPE_FILE),
                                                           node.getFileId()));
    File file = null;
    try {
      checkout.preprocess();
      file = File.createTempFile(checkout.getFileName(), ".txt");

      final FileOutputStream stream = new FileOutputStream(file);
      checkout.execute(stream);
    } catch (final Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    ret.put(ReturnValues.VALUES, file);
    return ret;
  }
}

{'fraction_non_alphanumeric': 0.11417283950617284, 'fraction_numerical': 0.004049382716049383, 'mean_word_length': 3.190151044899648, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3318680', 'n_tokens_mistral': 5856, 'n_tokens_neox': 5825, 'n_words': 1263}