package blade.migrate.liferay70;

import blade.migrate.api.FileMigrator;
import blade.migrate.api.Problem;
import blade.migrate.core.JavaFileChecker;
import blade.migrate.core.SearchResult;

import java.io.File;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;

import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;

public class JavaMethodMigrator implements FileMigrator {

	ComponentContext context;
	String methodType;
	String methodName;
	String methodExpression;
	String[] methodParamTypes;
	String problemTitle;
	String problemUrl;
	String problemSummary;
	String problemType;
	String problemTickets;

	@Activate
	public void activate(ComponentContext ctx) {
		this.context = ctx;

		final Dictionary<String, Object> properties =
			this.context.getProperties();

		this.methodType = (String)properties.get("method.type");
		this.methodName = (String)properties.get("method.name");
		this.methodExpression = (String)properties.get("method.expression");
		this.methodParamTypes =
			( (String)properties.get("method.param.types") ).split(",");
		this.problemTitle = (String)properties.get("problem.title");
		this.problemUrl = (String)properties.get("problem.url");
		this.problemSummary = (String)properties.get("problem.summary");
		this.problemType = (String)properties.get("problem.type");
		this.problemTickets = (String)properties.get("problem.tickets");
	}

	@Override
	public List<Problem> analyzeFile(File file) {
		final JavaFileChecker javaFileChecker = new JavaFileChecker(file);
		final List<Problem> problems = new ArrayList<>();

		SearchResult methodResult = null;

		if ("declaration".equals(methodType)) {
			methodResult = javaFileChecker.findMethodDeclartion(this.methodName,
				this.methodParamTypes);
		}
		else {
			methodResult = javaFileChecker.findMethodInvocation(this.methodName,
				this.methodExpression);
		}

		if (methodResult != null) {
			problems.add(new Problem(this.problemTitle, this.problemUrl, this.problemSummary,
				this.problemType, this.problemTickets, file, methodResult.startLine));
		}

		return problems;
	}

}
@@ -1880,36 +1880,42 @@
 tion(this.method
-Name
+Expression
 ,%0A%09%09%09%09this.metho
@@ -1915,26 +1915,20 @@
 s.method
-Expression
+Name
 );%0A%09%09%7D%0A%0A

fix method args