<commit_before>/*
 * Copyright 2017 StreamSets Inc.
 *
 * 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.
 */
package com.streamsets.datacollector.store.impl;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.streamsets.datacollector.config.PipelineConfiguration;
import com.streamsets.datacollector.config.RuleDefinitions;
import com.streamsets.datacollector.execution.StateEventListener;
import com.streamsets.datacollector.restapi.bean.UserJson;
import com.streamsets.datacollector.store.AclStoreTask;
import com.streamsets.datacollector.store.PipelineInfo;
import com.streamsets.datacollector.store.PipelineRevInfo;
import com.streamsets.datacollector.store.PipelineStoreException;
import com.streamsets.datacollector.store.PipelineStoreTask;
import com.streamsets.datacollector.util.PipelineException;
import com.streamsets.lib.security.acl.dto.Action;
import com.streamsets.lib.security.acl.dto.ResourceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

public class AclPipelineStoreTask implements PipelineStoreTask {
  private static final Logger LOG = LoggerFactory.getLogger(AclPipelineStoreTask.class);
  private final PipelineStoreTask pipelineStore;
  private final AclStoreTask aclStore;
  private final UserJson currentUser;

  @Inject
  public AclPipelineStoreTask(PipelineStoreTask pipelineStore, AclStoreTask aclStore, UserJson currentUser) {
    this.pipelineStore = pipelineStore;
    this.aclStore =  aclStore;
    this.currentUser = currentUser;
  }

  @Override
  public String getName() {
    return "AclPipelineStoreTask";
  }

  @Override
  public void init() {
    pipelineStore.init();
  }

  @Override
  public void run() {
    pipelineStore.run();
  }

  @Override
  public void waitWhileRunning() throws InterruptedException {
    pipelineStore.waitWhileRunning();
  }

  @Override
  public void stop() {
    pipelineStore.stop();
  }

  @Override
  public Status getStatus() {
    return pipelineStore.getStatus();
  }

  @Override
  public PipelineConfiguration create(
      String user,
      String pipelineId,
      String pipelineTitle,
      String description,
      boolean isRemote,
      boolean draft
  ) throws PipelineException {
    PipelineConfiguration pipelineConf = pipelineStore
        .create(user, pipelineId, pipelineTitle, description, isRemote, draft);
    aclStore.createAcl(pipelineId, ResourceType.PIPELINE, System.currentTimeMillis(), user);
    return pipelineConf;
  }

  @Override
  public void delete(String name) throws PipelineException {
    aclStore.validateWritePermission(name, currentUser);
    pipelineStore.delete(name);
    aclStore.deleteAcl(name);
  }

  @Override
  public List<PipelineInfo> getPipelines() throws PipelineStoreException {
    return new ArrayList<>(filterPipelineBasedOnReadAcl());
  }

  @Override
  public PipelineInfo getInfo(String name) throws PipelineException {
    aclStore.validateReadPermission(name, currentUser);
    return pipelineStore.getInfo(name);
  }

  @Override
  public List<PipelineRevInfo> getHistory(String name) throws PipelineException {
    aclStore.validateReadPermission(name, currentUser);
    return pipelineStore.getHistory(name);
  }

  @Override
  public PipelineConfiguration save(
      String user,<commit_msg>SDC-8629. Cannot create a pipeline: acl.json-tmp (No such file or directory)<commit_after>/*
 * Copyright 2017 StreamSets Inc.
 *
 * 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.
 */
package com.streamsets.datacollector.store.impl;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.streamsets.datacollector.config.PipelineConfiguration;
import com.streamsets.datacollector.config.RuleDefinitions;
import com.streamsets.datacollector.execution.StateEventListener;
import com.streamsets.datacollector.restapi.bean.UserJson;
import com.streamsets.datacollector.store.AclStoreTask;
import com.streamsets.datacollector.store.PipelineInfo;
import com.streamsets.datacollector.store.PipelineRevInfo;
import com.streamsets.datacollector.store.PipelineStoreException;
import com.streamsets.datacollector.store.PipelineStoreTask;
import com.streamsets.datacollector.util.PipelineException;
import com.streamsets.lib.security.acl.dto.Action;
import com.streamsets.lib.security.acl.dto.ResourceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

public class AclPipelineStoreTask implements PipelineStoreTask {
  private static final Logger LOG = LoggerFactory.getLogger(AclPipelineStoreTask.class);
  private final PipelineStoreTask pipelineStore;
  private final AclStoreTask aclStore;
  private final UserJson currentUser;

  @Inject
  public AclPipelineStoreTask(PipelineStoreTask pipelineStore, AclStoreTask aclStore, UserJson currentUser) {
    this.pipelineStore = pipelineStore;
    this.aclStore =  aclStore;
    this.currentUser = currentUser;
  }

  @Override
  public String getName() {
    return "AclPipelineStoreTask";
  }

  @Override
  public void init() {
    pipelineStore.init();
  }

  @Override
  public void run() {
    pipelineStore.run();
  }

  @Override
  public void waitWhileRunning() throws InterruptedException {
    pipelineStore.waitWhileRunning();
  }

  @Override
  public void stop() {
    pipelineStore.stop();
  }

  @Override
  public Status getStatus() {
    return pipelineStore.getStatus();
  }

  @Override
  public PipelineConfiguration create(
      String user,
      String pipelineId,
      String pipelineTitle,
      String description,
      boolean isRemote,
      boolean draft
  ) throws PipelineException {
    PipelineConfiguration pipelineConf = pipelineStore
        .create(user, pipelineId, pipelineTitle, description, isRemote, draft);
    if (!draft) {
      aclStore.createAcl(pipelineId, ResourceType.PIPELINE, System.currentTimeMillis(), user);
    }
    return pipelineConf;
  }

  @Override
  public void delete(String name) throws PipelineException {
    aclStore.validateWritePermission(name, currentUser);
    pipelineStore.delete(name);
    aclStore.deleteAcl(name);
  }

  @Override
  public List<PipelineInfo> getPipelines() throws PipelineStoreException {
    return new ArrayList<>(filterPipelineBasedOnReadAcl());
  }

  @Override
  public PipelineInfo getInfo(String name) throws PipelineException {
    aclStore.validateReadPermission(name, currentUser);
    return pipelineStore.getInfo(name);
  }

  @Override
  public List<PipelineRevInfo> getHistory(String name) throws PipelineException {
    aclStore.validateReadPermission(name, currentUser);
    return pipelineStore.getHistory(name);
  }

  @Override
  public PipelineConfiguration save(
      String user,
{'fraction_non_alphanumeric': 0.0826060233558697, 'fraction_numerical': 0.0031960663798401966, 'mean_word_length': 4.7014716187806584, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6004324', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2282, 'n_words': 578}