Cursling.deleteMonitor.java Source code

Java tutorial

Introduction

Here is the source code for Cursling.deleteMonitor.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Cursling;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author Ajit (P7109156) <Ness>
 */
public class deleteMonitor {

    static final Logger logger = Logger.getLogger(deleteMonitor.class);
    public static String input_file_del;
    public static String output_file_del;
    public File storeUpdate;
    public File storeUpdate1;
    boolean isdeleted;
    boolean isdeleted1;

    public static void main(String[] args) {

    }

    public String _deleteMonitor(String monitor_name) {
        isdeleted = false;
        try {
            File inFile = new File(input_file_del);

            if (!inFile.isFile()) {
                logger.info("Failed to Delete. Data file not found.");
                return ("failed");
            }
            storeUpdate = new File(input_file_del);
            File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
            BufferedReader br = new BufferedReader(new FileReader(input_file_del));
            PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
            String line = null;

            while ((line = br.readLine()) != null) {
                if (!line.equalsIgnoreCase("")) {
                    String tmp_linee = line.replaceAll("\\\\", "");
                    if (tmp_linee.startsWith(monitor_name + " = ht")
                            || tmp_linee.startsWith(monitor_name + "=ht")) {
                        isdeleted = true;
                        pw.flush();
                    } else if (tmp_linee.startsWith(monitor_name.toLowerCase() + " = ena")
                            || tmp_linee.startsWith(monitor_name.toLowerCase() + " = dis")
                            || tmp_linee.startsWith(monitor_name.toLowerCase() + "=ena")
                            || tmp_linee.startsWith(monitor_name.toLowerCase() + "=dis")) {
                        isdeleted = true;
                        pw.flush();
                    } else {
                        pw.println(line);
                        pw.flush();
                    }

                } else {
                }
            }

            pw.close();
            br.close();

            //Rename the new file to the filename the original file had.
            if (tempFile.canRead()) {
                Long before = storeUpdate.lastModified();
                FileUtils.copyFile(tempFile, storeUpdate);
                Long after = storeUpdate.lastModified();
                if (before.equals(after)) {
                    logger.info("failed to delete Monitor " + monitor_name);
                    return ("failed");
                } else if (isdeleted == true && deleteOutput(monitor_name) == true) {
                    logger.info("Monitor " + monitor_name + " Deleted.");
                    return ("deleted");
                } else if (isdeleted == true && deleteOutput(monitor_name) == false) {
                    logger.info("Monitor " + monitor_name + " Deleted. But output will removed in Sometime.");
                    return ("deleted");
                } else {
                    logger.info("Failed to delete Monitor " + monitor_name + " . Monitor not found.");
                    return ("not_found");
                }
            } else {
                logger.info("failed to delete Monitor " + monitor_name);
                return ("failed");
            }
        } catch (FileNotFoundException ex) {
            logger.info("failed to delete Monitor " + monitor_name);
            logger.error("Error1 : " + ex);
            return ("failed");
        } catch (IOException ex) {
            logger.info("failed to delete Monitor " + monitor_name);
            logger.error("Error2 : " + ex);
            return ("failed");
        }
    }

    public boolean deleteOutput(String monitor_name) {
        isdeleted1 = false;
        try {
            File inFile = new File(output_file_del);

            if (!inFile.isFile()) {
                logger.info("Parameter is not an existing file");
                return false;
            }

            storeUpdate1 = new File(output_file_del);

            File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
            BufferedReader br = new BufferedReader(new FileReader(output_file_del));
            PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

            String line = null;

            while ((line = br.readLine()) != null) {
                if (!line.equalsIgnoreCase("")) {
                    if (line.startsWith("[\"" + monitor_name + "\"")) {

                        line = line.replaceAll(monitor_name, "hide");
                        pw.println(line);
                        pw.flush();
                        isdeleted1 = true;
                    } else {
                        pw.println(line);
                        pw.flush();
                    }
                } else {
                }
            }
            pw.close();
            br.close();

            //Rename the new file to the filename the original file had.
            if (tempFile.canRead()) {
                Long before = storeUpdate1.lastModified();
                FileUtils.copyFile(tempFile, storeUpdate1);
                Long after = storeUpdate1.lastModified();
                if (before.equals(after)) {
                    return false;
                } else if (isdeleted1) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } catch (FileNotFoundException ex) {
            logger.error("Error1 : " + ex);
            return false;

        } catch (IOException ex) {
            logger.error("Error2 : " + ex);
            return false;
        }
    }
}