<commit_before>/* 
 * Copyright 2016 <NAME>.
 *
 * 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 se.trixon.filebydate;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.PathMatcher;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import se.trixon.util.BundleHelper;
import se.trixon.util.Xlog;
import se.trixon.util.dictionary.Dict;

/**
 *
 * @author <NAME>
 */
public class Operation {

    private final ResourceBundle mBundle;
    private final List<Exception> mExceptions = new ArrayList<>();
    private final List<File> mFiles = new ArrayList<>();
    private boolean mInterrupted;
    private final OperationListener mListener;
    private final OptionsHolder mOptionsHolder;

    public Operation(OperationListener operationListener, OptionsHolder optionsHolder) {
        mListener = operationListener;
        mOptionsHolder = optionsHolder;
        mBundle = BundleHelper.getBundle(Operation.class, "Bundle");
    }

    public void start() {
        mListener.onOperationStarted();
        long startTime = System.currentTimeMillis();
        mInterrupted = !generateFileList();
        String status;

        if (!mInterrupted && !mFiles.isEmpty()) {
            status = Dict.PROCESSING.toString();
            mListener.onOperationLog(status);

            for (File sourceFile : mFiles) {
                try {
                    SimpleDateFormat simpleDateFormat = mOptionsHolder.getDateFormat();

                    String fileDate = simpleDateFormat.format(getDate(sourceFile));
                    File destDir = new File(mOptionsHolder.getDestDir(), fileDate);

                    if (destDir.isFile()) {
                        mListener.onOperationLog(String.format(mBundle.getString("err_dest_dir_is_file"), destDir.getAbsolutePath()));
                        break;
                    } else if (!destDir.exists() && !mOptionsHolder.isDryRun()) {
                        FileUtils.forceMkdir(destDir);
                    }

                    File destFile = new File(destDir, sourceFile.getName());
                    String log;
                    if (destFile.exists() && !mOptionsHolder.isReplaceExisting()) {
                        log = String.format(mBundle.getString("err_dest_file_exists"), destFile.getAbsolutePath());
                    } else {
                        Command command = mOptionsHolder.getCommand();
                        String cmd = command == Command.COPY ? "cp" : "mv";
                        log = String.format("%s %s    %s", cmd, sourceFile.getAbsolutePath(), destFile.toString());

                        if (destDir.canWrite()) {
                            if (!mOptionsHolder.isDryRun()) {
                                if (command == Command.COPY) {
                                    FileUtils.copyFile(sourceFile, destFile);
                                } else if (command == Command.MOVE) {
                                    FileUtils.moveFile(sourceFile, destFile);
                                }
                            }<commit_msg>Implement case change<commit_after>/* 
 * Copyright 2016 <NAME>.
 *
 * 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 se.trixon.filebydate;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.PathMatcher;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import se.trixon.util.BundleHelper;
import se.trixon.util.Xlog;
import se.trixon.util.dictionary.Dict;

/**
 *
 * @author <NAME>
 */
public class Operation {

    private final ResourceBundle mBundle;
    private final List<Exception> mExceptions = new ArrayList<>();
    private final List<File> mFiles = new ArrayList<>();
    private boolean mInterrupted;
    private final OperationListener mListener;
    private final OptionsHolder mOptionsHolder;

    public Operation(OperationListener operationListener, OptionsHolder optionsHolder) {
        mListener = operationListener;
        mOptionsHolder = optionsHolder;
        mBundle = BundleHelper.getBundle(Operation.class, "Bundle");
    }

    public void start() {
        mListener.onOperationStarted();
        long startTime = System.currentTimeMillis();
        mInterrupted = !generateFileList();
        String status;

        if (!mInterrupted && !mFiles.isEmpty()) {
            status = Dict.PROCESSING.toString();
            mListener.onOperationLog(status);

            for (File sourceFile : mFiles) {
                try {
                    SimpleDateFormat simpleDateFormat = mOptionsHolder.getDateFormat();

                    String fileDate = simpleDateFormat.format(getDate(sourceFile));
                    File destDir = new File(mOptionsHolder.getDestDir(), fileDate);

                    if (destDir.isFile()) {
                        mListener.onOperationLog(String.format(mBundle.getString("err_dest_dir_is_file"), destDir.getAbsolutePath()));
                        break;
                    } else if (!destDir.exists() && !mOptionsHolder.isDryRun()) {
                        FileUtils.forceMkdir(destDir);
                    }

                    String destFilename = sourceFile.getName();
                    String base = FilenameUtils.getBaseName(destFilename);
                    String ext = FilenameUtils.getExtension(destFilename);
                    NameCase baseCase = mOptionsHolder.getBaseNameCase();
                    NameCase extCase = mOptionsHolder.getExtNameCase();

                    if (baseCase != null || extCase != null) {
                        if (baseCase != null) {
                            base = baseCase == NameCase.LOWER ? base.toLowerCase() : base.toUpperCase();
                        }

                        if (extCase != null) {
                            ext = extCase == NameCase.LOWER ? ext.toLowerCase() : ext.toUpperCase();
                        }

                        if (base.length() == 0) {
                            destFilename = String.format(".%s", ext);
                        } else if (ext.length() == 0) {
                            destFilename = base;
                        } else {
                            destFilename = String.format("%s.%s", base, ext);
                        }
                    }

                    File destFile = new File(destDir, destFilename);
                    String log;
                    if (destFile.exists() && !mOptionsHolder.isReplaceExisting()) {
                        log = String.format(mBundle.getString("err_dest_file_exists"), destFile.getAbsolutePath());
                    } else {
                        Command command = mOptionsHolder.getCommand();
                        String cmd = command == Command.COPY ? "cp" : "mv";
                        log = String.format("%s %s  %s", cmd, sourceFile.getAbsolutePath(), destFile.toString());

                        if (destDir.canWrite()) {
                            if (!mOptionsHolder.isDryRun()) {
                                if (command == Command.COPY) {
                                    FileUtils.copyFile(sourceFile, destFile);
                                } else if (command == Command.MOVE) {
                                    FileUtils.moveFile(sourceFile, destFile);
                                }
                            }
{'fraction_non_alphanumeric': 0.09713140687618646, 'fraction_numerical': 0.0018983336848766082, 'mean_word_length': 2.073905996758509, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1556088', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2306, 'n_words': 581}