Search in sources :

Example 1 with AutoMigrator

use of com.liferay.blade.api.AutoMigrator in project liferay-ide by liferay.

the class AutoCorrectAction method runWithAutoCorrect.

public IStatus runWithAutoCorrect(final List<Problem> problems) {
    final IResource file = MigrationUtil.getIResourceFromProblem(problems.get(0));
    final BundleContext context = FrameworkUtil.getBundle(AutoCorrectAction.class).getBundleContext();
    WorkspaceJob job = new WorkspaceJob("Auto correcting migration problem.") {

        @Override
        public IStatus runInWorkspace(IProgressMonitor monitor) {
            IStatus retval = Status.OK_STATUS;
            try {
                final Problem problem = problems.get(0);
                String autoCorrectKey = null;
                final int filterKeyIndex = problem.autoCorrectContext.indexOf(":");
                if (filterKeyIndex > -1) {
                    autoCorrectKey = problem.autoCorrectContext.substring(0, filterKeyIndex);
                } else {
                    autoCorrectKey = problem.autoCorrectContext;
                }
                final Collection<ServiceReference<AutoMigrator>> refs = context.getServiceReferences(AutoMigrator.class, "(auto.correct=" + autoCorrectKey + ")");
                for (ServiceReference<AutoMigrator> ref : refs) {
                    final AutoMigrator autoMigrator = context.getService(ref);
                    int problemsCorrected = autoMigrator.correctProblems(problem.file, Collections.singletonList(problem));
                    if (problemsCorrected > 0) {
                        IResource resource = MigrationUtil.getIResourceFromProblem(problem);
                        if (resource != null) {
                            IMarker problemMarker = resource.findMarker(problem.markerId);
                            if ((problemMarker != null) && problemMarker.exists()) {
                                problemMarker.delete();
                            }
                        }
                    }
                }
                file.refreshLocal(IResource.DEPTH_ONE, monitor);
                MigrateProjectHandler migrateHandler = new MigrateProjectHandler();
                Path path = new Path(problem.getFile().getPath());
                String projectName = "";
                IProject project = CoreUtil.getProject(problem.getFile());
                if (project.exists() && (project != null)) {
                    projectName = project.getName();
                }
                for (Problem p : problems) {
                    new MarkDoneAction().run(p, _provider);
                }
                if (!projectName.equals("")) {
                    migrateHandler.findMigrationProblems(new Path[] { path }, new String[] { projectName });
                }
            } catch (AutoMigrateException | CoreException | InvalidSyntaxException e) {
                return retval = ProjectUI.createErrorStatus("Unable to auto correct problem", e);
            }
            return retval;
        }
    };
    job.schedule();
    return Status.OK_STATUS;
}
Also used : Path(org.eclipse.core.runtime.Path) IStatus(org.eclipse.core.runtime.IStatus) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IProject(org.eclipse.core.resources.IProject) ServiceReference(org.osgi.framework.ServiceReference) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) AutoMigrator(com.liferay.blade.api.AutoMigrator) Problem(com.liferay.blade.api.Problem) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IMarker(org.eclipse.core.resources.IMarker) IResource(org.eclipse.core.resources.IResource) BundleContext(org.osgi.framework.BundleContext)

Example 2 with AutoMigrator

use of com.liferay.blade.api.AutoMigrator in project liferay-ide by liferay.

the class AutoCorrectAllAction method run.

public void run() {
    final BundleContext context = FrameworkUtil.getBundle(AutoCorrectAction.class).getBundleContext();
    WorkspaceJob job = new WorkspaceJob("Auto correcting all of migration problem.") {

        @Override
        public IStatus runInWorkspace(IProgressMonitor monitor) {
            IStatus retval = Status.OK_STATUS;
            try {
                if ((_problemsContainerList != null) && (_problemsContainerList.size() > 0)) {
                    for (ProblemsContainer problemsContainer : _problemsContainerList) {
                        for (UpgradeProblems upgradeProblems : problemsContainer.getProblemsArray()) {
                            FileProblems[] fileProblemsArray = upgradeProblems.getProblems();
                            for (FileProblems fileProblems : fileProblemsArray) {
                                List<Problem> problems = fileProblems.getProblems();
                                Set<String> fixed = new HashSet<>();
                                for (Problem problem : problems) {
                                    if (problem.getStatus() == Problem.STATUS_IGNORE) {
                                        continue;
                                    }
                                    final IResource file = MigrationUtil.getIResourceFromProblem(problem);
                                    if (FileUtil.notExists(file)) {
                                        continue;
                                    }
                                    String fixedKey = file.getLocation().toString() + "," + problem.autoCorrectContext;
                                    if ((problem.autoCorrectContext == null) || fixed.contains(fixedKey)) {
                                        continue;
                                    }
                                    String autoCorrectKey = null;
                                    final int filterKeyIndex = problem.autoCorrectContext.indexOf(":");
                                    if (filterKeyIndex > -1) {
                                        autoCorrectKey = problem.autoCorrectContext.substring(0, filterKeyIndex);
                                    } else {
                                        autoCorrectKey = problem.autoCorrectContext;
                                    }
                                    final Collection<ServiceReference<AutoMigrator>> refs = context.getServiceReferences(AutoMigrator.class, "(auto.correct=" + autoCorrectKey + ")");
                                    for (ServiceReference<AutoMigrator> ref : refs) {
                                        final AutoMigrator autoMigrator = context.getService(ref);
                                        int problemsCorrected = autoMigrator.correctProblems(problem.file, problems);
                                        fixed.add(fixedKey);
                                        if ((problemsCorrected > 0) && (file != null)) {
                                            IMarker problemMarker = file.findMarker(problem.markerId);
                                            if ((problemMarker != null) && problemMarker.exists()) {
                                                problemMarker.delete();
                                            }
                                        }
                                    }
                                    file.refreshLocal(IResource.DEPTH_ONE, monitor);
                                }
                            }
                        }
                    }
                }
                UIUtil.sync(new Runnable() {

                    @Override
                    public void run() {
                        IViewPart view = UIUtil.findView(UpgradeView.ID);
                        try {
                            BreakingChangeSelectedProject selectedProject = UpgradeAssistantSettingsUtil.getObjectFromStore(BreakingChangeSelectedProject.class);
                            StructuredSelection projectSelection = null;
                            List<IProject> projects = new ArrayList<>();
                            if (selectedProject != null) {
                                List<BreakingChangeSimpleProject> selectedProjects = selectedProject.getSelectedProjects();
                                selectedProjects.stream().forEach(breakingProject -> projects.add(CoreUtil.getProject(breakingProject.getName())));
                                projectSelection = new StructuredSelection(projects.toArray(new IProject[0]));
                            }
                            new RunMigrationToolAction("Run Migration Tool", view.getViewSite().getShell(), projectSelection).run();
                        } catch (IOException ioe) {
                            ProjectUI.logError(ioe);
                        }
                    }
                });
            } catch (AutoMigrateException | CoreException | InvalidSyntaxException e) {
                return retval = ProjectUI.createErrorStatus("Unable to auto correct problem", e);
            }
            return retval;
        }
    };
    job.schedule();
}
Also used : CoreUtil(com.liferay.ide.core.util.CoreUtil) CoreException(org.eclipse.core.runtime.CoreException) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IStatus(org.eclipse.core.runtime.IStatus) UpgradeAssistantSettingsUtil(com.liferay.ide.project.core.upgrade.UpgradeAssistantSettingsUtil) IProject(org.eclipse.core.resources.IProject) IViewPart(org.eclipse.ui.IViewPart) IMarker(org.eclipse.core.resources.IMarker) ServiceReference(org.osgi.framework.ServiceReference) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FileUtil(com.liferay.ide.core.util.FileUtil) UpgradeProblems(com.liferay.ide.project.core.upgrade.UpgradeProblems) Collection(java.util.Collection) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) IOException(java.io.IOException) Action(org.eclipse.jface.action.Action) ProjectUI(com.liferay.ide.project.ui.ProjectUI) BundleContext(org.osgi.framework.BundleContext) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Problem(com.liferay.blade.api.Problem) BreakingChangeSimpleProject(com.liferay.ide.project.core.upgrade.BreakingChangeSimpleProject) List(java.util.List) AutoMigrator(com.liferay.blade.api.AutoMigrator) UIUtil(com.liferay.ide.ui.util.UIUtil) IResource(org.eclipse.core.resources.IResource) BreakingChangeSelectedProject(com.liferay.ide.project.core.upgrade.BreakingChangeSelectedProject) FileProblems(com.liferay.ide.project.core.upgrade.FileProblems) UpgradeView(com.liferay.ide.project.ui.upgrade.animated.UpgradeView) ProblemsContainer(com.liferay.ide.project.core.upgrade.ProblemsContainer) FrameworkUtil(org.osgi.framework.FrameworkUtil) IStatus(org.eclipse.core.runtime.IStatus) IViewPart(org.eclipse.ui.IViewPart) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ProblemsContainer(com.liferay.ide.project.core.upgrade.ProblemsContainer) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BreakingChangeSelectedProject(com.liferay.ide.project.core.upgrade.BreakingChangeSelectedProject) HashSet(java.util.HashSet) UpgradeProblems(com.liferay.ide.project.core.upgrade.UpgradeProblems) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) ServiceReference(org.osgi.framework.ServiceReference) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileProblems(com.liferay.ide.project.core.upgrade.FileProblems) CoreException(org.eclipse.core.runtime.CoreException) AutoMigrator(com.liferay.blade.api.AutoMigrator) Problem(com.liferay.blade.api.Problem) IMarker(org.eclipse.core.resources.IMarker) IResource(org.eclipse.core.resources.IResource) BundleContext(org.osgi.framework.BundleContext)

Example 3 with AutoMigrator

use of com.liferay.blade.api.AutoMigrator in project liferay-ide by liferay.

the class JSPTagImportsTest method allProblems.

@Test
public void allProblems() throws Exception {
    ServiceReference<Migration> sr = context.getServiceReference(Migration.class);
    Migration migration = context.getService(sr);
    File tempFolder = Files.createTempDirectory("autocorrect").toFile();
    File testFile = new File(tempFolder, "jsptaglist.jsp");
    tempFolder.deleteOnExit();
    File originalTestfile = new File("jsptests/imports/view.jsp");
    Files.copy(originalTestfile.toPath(), testFile.toPath());
    List<Problem> problems = migration.findProblems(testFile, new NullProgressMonitor());
    assertEquals(6, problems.size());
    Collection<ServiceReference<AutoMigrator>> refs = context.getServiceReferences(AutoMigrator.class, "(auto.correct=import)");
    for (ServiceReference<AutoMigrator> ref : refs) {
        AutoMigrator autoMigrator = context.getService(ref);
        autoMigrator.correctProblems(testFile, problems);
    }
    File dest = new File(tempFolder, "Updated.jsp");
    assertTrue(testFile.renameTo(dest));
    List<Problem> problems2 = migration.findProblems(dest, new NullProgressMonitor());
    assertEquals(0, problems2.size());
}
Also used : NullProgressMonitor(com.liferay.blade.util.NullProgressMonitor) Migration(com.liferay.blade.api.Migration) AutoMigrator(com.liferay.blade.api.AutoMigrator) Problem(com.liferay.blade.api.Problem) File(java.io.File) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with AutoMigrator

use of com.liferay.blade.api.AutoMigrator in project liferay-ide by liferay.

the class MVCPortletClassInPortletXMLAutoCorrectTest method beforeTest.

@Before
public void beforeTest() throws Exception {
    Filter filter = context.createFilter("(implName=MVCPortletClassInPortletXML)");
    ServiceTracker<AutoMigrator, AutoMigrator> tracker = new ServiceTracker<AutoMigrator, AutoMigrator>(context, filter, null);
    tracker.open();
    ServiceReference<AutoMigrator>[] refs = tracker.getServiceReferences();
    assertNotNull(refs);
    assertEquals(1, refs.length);
    autoMigrator = context.getService(refs[0]);
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) AutoMigrator(com.liferay.blade.api.AutoMigrator) ServiceReference(org.osgi.framework.ServiceReference) Before(org.junit.Before)

Example 5 with AutoMigrator

use of com.liferay.blade.api.AutoMigrator in project liferay-ide by liferay.

the class PortalServiceImportsAutoCorrectTest method autoCorrectProblems.

@Test
public void autoCorrectProblems() throws Exception {
    File tempFolder = Files.createTempDirectory("autocorrect").toFile();
    File testFile = new File(tempFolder, "PortalServiceImports.java");
    tempFolder.deleteOnExit();
    File originalTestfile = new File("javatests/PortalServiceImports.java");
    Files.copy(originalTestfile.toPath(), testFile.toPath());
    List<Problem> problems = null;
    FileMigrator migrator = null;
    Collection<ServiceReference<FileMigrator>> mrefs = context.getServiceReferences(FileMigrator.class, null);
    for (ServiceReference<FileMigrator> mref : mrefs) {
        migrator = context.getService(mref);
        if (migrator.getClass().getName().contains("PortalServiceImports")) {
            problems = migrator.analyze(testFile);
            break;
        }
    }
    assertEquals(2, problems.size());
    int problemsFixed = ((AutoMigrator) migrator).correctProblems(testFile, problems);
    assertEquals(2, problemsFixed);
    File dest = new File(tempFolder, "Updated.java");
    assertTrue(testFile.renameTo(dest));
    problems = migrator.analyze(dest);
    assertEquals(0, problems.size());
}
Also used : FileMigrator(com.liferay.blade.api.FileMigrator) AutoMigrator(com.liferay.blade.api.AutoMigrator) Problem(com.liferay.blade.api.Problem) File(java.io.File) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

AutoMigrator (com.liferay.blade.api.AutoMigrator)8 ServiceReference (org.osgi.framework.ServiceReference)8 Problem (com.liferay.blade.api.Problem)7 File (java.io.File)5 Test (org.junit.Test)5 FileMigrator (com.liferay.blade.api.FileMigrator)4 AutoMigrateException (com.liferay.blade.api.AutoMigrateException)2 IMarker (org.eclipse.core.resources.IMarker)2 IProject (org.eclipse.core.resources.IProject)2 IResource (org.eclipse.core.resources.IResource)2 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)2 CoreException (org.eclipse.core.runtime.CoreException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 BundleContext (org.osgi.framework.BundleContext)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 Migration (com.liferay.blade.api.Migration)1 NullProgressMonitor (com.liferay.blade.util.NullProgressMonitor)1 CoreUtil (com.liferay.ide.core.util.CoreUtil)1 FileUtil (com.liferay.ide.core.util.FileUtil)1