Monday, August 13, 2018

Add @Controller annotation to given classes

package com.advitha.util;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.SetUtils;
import org.apache.commons.lang.SerializationUtils;

public class AddLinesOfCodeToClasses {

public static Set<String> controllerClassesWithPackageInfo = null;
public static Set<String> controllerClasses = null;
public static Set<String> classesFound = new HashSet<>();
static {
controllerClassesWithPackageInfo = getControllerClassesWithPackageInfo();
controllerClasses = getControllerClasses(controllerClassesWithPackageInfo);
System.out.println("Set Size: " + controllerClasses.size());
}
    public static int counter = 0;
   

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        File projectHome = null;

        do {
            System.out.println("Enter Project Home Path: ");
            String projectHomeLocation = scanner.nextLine();
            projectHome = new File(projectHomeLocation);

            if (!projectHome.exists()) {
                System.out.println("Project home specfied doesn't exists!!!");
            }
            if (projectHome.exists() && !projectHome.isDirectory()) {
                System.out.println("Select path to a directory!!!");
            }

        } while (!projectHome.exists() || !projectHome.isDirectory());

        listFilesForFolder(projectHome);

        System.out.println("\nYour project has " + counter
                + " lines of Java code!!!");
        System.out.println("Classes Found: " + classesFound.size());
        List<String> classesMissing = (java.util.List<String>) CollectionUtils.disjunction(controllerClassesWithPackageInfo, classesFound);
        for(String name: classesMissing) {
        System.out.println(name);
        }
    }

    public static void listFilesForFolder(final File folder) throws IOException {

        for (final File fileEntry : folder.listFiles()) {

            if (fileEntry.isDirectory()) {
               
                listFilesForFolder(fileEntry);
            } else {

                if (fileEntry.getName().contains(".java")) {
                if(controllerClasses.contains(getFileNameWithoutExtension(fileEntry.getName()))){
               
                Path FILE_PATH = Paths.get(fileEntry.getPath());
                List<String> fileContent = new ArrayList<>(Files.readAllLines(FILE_PATH, StandardCharsets.UTF_8));

                if(fileContent.contains("@Controller")) {
                System.out.println("@Controller annotation is already available!" + fileEntry.getName());
                continue;
                }
                boolean packageDefFound = false;
                boolean packageMatchFound = false;
                String clsNmWithPkgPfx = null;
                for (int i = 0; i < fileContent.size(); i++) {
               
                if(packageDefFound == false && fileContent.get(i).contains("package ")) {
                String packageDefLine = fileContent.get(i).trim();
                clsNmWithPkgPfx = getPacakgeNameFromPackageDeclaration(packageDefLine) + "." + getFileNameWithoutExtension(fileEntry.getName());
                if(controllerClassesWithPackageInfo.contains(clsNmWithPkgPfx)) {
                packageMatchFound = true;
                }
                packageDefFound = true;
                continue;
                }
               
                if(packageDefFound == true && packageMatchFound == false) {
                break;
                }
                    if (packageMatchFound == true && fileContent.get(i).contains("class " + getFileNameWithoutExtension(fileEntry.getName()))) {
                        fileContent.set(i, "import org.springframework.stereotype.Controller;" + System.lineSeparator() + "@Controller"+ System.lineSeparator() + fileContent.get(i));
                    classesFound.add(clsNmWithPkgPfx);
//                    counter++;
                        break;
                    }
                }

                Files.write(FILE_PATH, fileContent, StandardCharsets.UTF_8);
               
                }
                }
            }
        }
    }

    public static boolean readInput(String inputMsg, Scanner scanner) {
        String temp = null;
       
        do {
           
            System.out.println(inputMsg);
            temp = scanner.nextLine().trim();
           
            if (temp.matches("[^yYnN]")) {
                System.out.println("Invalid input!!!");
            }
           
        } while (temp.matches("[^yYnN]"));

        return getBoolValue(temp);
    }

    public static boolean getBoolValue(String input) {
        return input.matches("[yY]") ? true : false;
    }
   
    public static Set<String> getControllerClassesWithPackageInfo() {
String controllerClassesString = "com.advitha.controllers.Controller1, com.advitha.controllers.Controller2, com.advitha.controllers.Controller3";
String[] controllerClassesArray = controllerClassesString.split(", ");

Set<String> controllerClassesSet = new HashSet<>(Arrays.asList(controllerClassesArray));
return controllerClassesSet;
}
   
    public static Set<String> getControllerClasses(Set<String> classNamesWithPackagePrefixes) {
    Set<String> controllerClasses = new HashSet<>();
    for(String name: classNamesWithPackagePrefixes) {
    controllerClasses.add(getClassNameFromAbsoluteName(name));
    }
    return controllerClasses;
    }
   
    public static String getClassNameFromAbsoluteName(String absoluteFileName) {
return absoluteFileName.substring(absoluteFileName.lastIndexOf(".")+1);
}
   
    public static String getFileNameWithoutExtension(String fileName) {
return fileName.substring(0, fileName.lastIndexOf("."));
}
   
    public static String getPacakgeNameFromPackageDeclaration(String line) {
return line.substring(line.indexOf(" ")).trim().replaceAll(";", "");
}
}