Search in sources :

Example 1 with UpgradeProblems

use of com.liferay.ide.project.core.upgrade.UpgradeProblems in project liferay-ide by liferay.

the class MigrateProjectHandler method findMigrationProblems.

public void findMigrationProblems(final IPath[] locations, final String[] projectName) {
    Job job = new WorkspaceJob("Finding migration problems...") {

        @Override
        public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
            IStatus retval = Status.OK_STATUS;
            final BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
            ProgressMonitor override = new ProgressMonitor() {

                @Override
                public void beginTask(String taskName, int totalWork) {
                    monitor.beginTask(taskName, totalWork);
                }

                @Override
                public void done() {
                    monitor.done();
                }

                @Override
                public boolean isCanceled() {
                    return monitor.isCanceled();
                }

                @Override
                public void setTaskName(String taskName) {
                    monitor.setTaskName(taskName);
                }

                @Override
                public void worked(int work) {
                    monitor.worked(work);
                }
            };
            try {
                final ServiceReference<Migration> sr = context.getServiceReference(Migration.class);
                final Migration m = context.getService(sr);
                List<Problem> allProblems = null;
                boolean singleFile = false;
                if ((locations.length == 1) && locations[0].toFile().exists() && locations[0].toFile().isFile()) {
                    singleFile = true;
                }
                MigrationProblemsContainer container = null;
                if (_combineExistedProblem == true) {
                    container = UpgradeAssistantSettingsUtil.getObjectFromStore(MigrationProblemsContainer.class);
                }
                List<MigrationProblems> migrationProblemsList = new ArrayList<>();
                if (container == null) {
                    container = new MigrationProblemsContainer();
                }
                if (container.getProblemsArray() != null) {
                    List<MigrationProblems> mpList = Arrays.asList(container.getProblemsArray());
                    for (MigrationProblems mp : mpList) {
                        migrationProblemsList.add(mp);
                    }
                }
                for (int j = 0; j < locations.length; j++) {
                    allProblems = new ArrayList<>();
                    if (!override.isCanceled() && _shouldSearch(locations[j].toFile())) {
                        List<Problem> problems = null;
                        if (singleFile) {
                            _clearFileMarkers(locations[j].toFile());
                            Set<File> files = new HashSet<>();
                            files.add(locations[j].toFile());
                            problems = m.findProblems(files, override);
                        } else {
                            problems = m.findProblems(locations[j].toFile(), override);
                        }
                        for (Problem problem : problems) {
                            if (_shouldAdd(problem)) {
                                allProblems.add(problem);
                            }
                        }
                    }
                    if (ListUtil.isNotEmpty(allProblems)) {
                        _addMarkers(allProblems);
                        MigrationProblems migrationProblems = new MigrationProblems();
                        FileProblems[] fileProblems = FileProblemsUtil.newFileProblemsListFrom(allProblems.toArray(new Problem[0]));
                        migrationProblems.setProblems(fileProblems);
                        migrationProblems.setType("Code Problems");
                        migrationProblems.setSuffix(projectName[j]);
                        int index = _isAlreadyExist(migrationProblemsList, projectName, j);
                        if (index != -1) {
                            if (singleFile) {
                                UpgradeProblems up = migrationProblemsList.get(index);
                                FileProblems[] problems = up.getProblems();
                                for (int n = 0; n < problems.length; n++) {
                                    FileProblems fp = problems[n];
                                    String problemFilePath = fp.getFile().getPath();
                                    String initProblemFilePath = locations[0].toFile().getPath();
                                    if (problemFilePath.equals(initProblemFilePath)) {
                                        problems[n] = fileProblems[0];
                                        break;
                                    }
                                }
                                migrationProblems.setProblems(problems);
                            }
                            migrationProblemsList.set(index, migrationProblems);
                        } else {
                            migrationProblemsList.add(migrationProblems);
                        }
                    } else {
                        int index = _isAlreadyExist(migrationProblemsList, projectName, j);
                        if (index != -1) {
                            if (singleFile) {
                                MigrationProblems mp = migrationProblemsList.get(index);
                                FileProblems[] fps = mp.getProblems();
                                List<FileProblems> fpList = Arrays.asList(fps);
                                List<FileProblems> newFPList = new ArrayList<>();
                                for (FileProblems fp : fpList) {
                                    String problemFilePath = fp.getFile().getPath();
                                    String initProblemFilePath = locations[0].toFile().getPath();
                                    if (!problemFilePath.equals(initProblemFilePath)) {
                                        newFPList.add(fp);
                                    }
                                }
                                mp.setProblems(newFPList.toArray(new FileProblems[0]));
                                migrationProblemsList.set(index, mp);
                            } else {
                                migrationProblemsList.remove(index);
                            }
                        }
                    }
                }
                if (ListUtil.isNotEmpty(migrationProblemsList)) {
                    container.setProblemsArray(migrationProblemsList.toArray(new MigrationProblems[0]));
                    UpgradeAssistantSettingsUtil.setObjectToStore(MigrationProblemsContainer.class, container);
                } else {
                    UpgradeAssistantSettingsUtil.setObjectToStore(MigrationProblemsContainer.class, null);
                }
                if (singleFile) {
                    refreshViewer(allProblems);
                } else {
                    allProblems.add(new Problem());
                    m.reportProblems(allProblems, Migration.DETAIL_LONG, "ide");
                }
            } catch (Exception e) {
                retval = ProjectUI.createErrorStatus("Error in migrate command", e);
            }
            return retval;
        }
    };
    try {
        ProjectUI.getProgressService().showInDialog(Display.getDefault().getActiveShell(), job);
    } catch (Exception e) {
    }
    job.schedule();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Migration(com.liferay.blade.api.Migration) ArrayList(java.util.ArrayList) MigrationProblems(com.liferay.ide.project.core.upgrade.MigrationProblems) MigrationProblemsContainer(com.liferay.ide.project.core.upgrade.MigrationProblemsContainer) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Job(org.eclipse.core.runtime.jobs.Job) HashSet(java.util.HashSet) UpgradeProblems(com.liferay.ide.project.core.upgrade.UpgradeProblems) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) CoreException(org.eclipse.core.runtime.CoreException) ExecutionException(org.eclipse.core.commands.ExecutionException) ProgressMonitor(com.liferay.blade.api.ProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileProblems(com.liferay.ide.project.core.upgrade.FileProblems) Problem(com.liferay.blade.api.Problem) IFile(org.eclipse.core.resources.IFile) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 2 with UpgradeProblems

use of com.liferay.ide.project.core.upgrade.UpgradeProblems 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 UpgradeProblems

use of com.liferay.ide.project.core.upgrade.UpgradeProblems in project liferay-ide by liferay.

the class MigrationContentProvider method hasChildren.

@Override
public boolean hasChildren(Object element) {
    if (element instanceof ProblemsContainer) {
        ProblemsContainer problemsContainer = (ProblemsContainer) element;
        UpgradeProblems[] upgradeProblemsArray = problemsContainer.getProblemsArray();
        if (upgradeProblemsArray.length == 1) {
            if (ListUtil.isNotEmpty(problemsContainer.getProblemsArray()[0].getProblems())) {
                return true;
            }
            return false;
        } else {
            return true;
        }
    } else if (element instanceof UpgradeProblems) {
        if (ListUtil.isNotEmpty(((UpgradeProblems) element).getProblems())) {
            return true;
        }
        return false;
    }
    return false;
}
Also used : UpgradeProblems(com.liferay.ide.project.core.upgrade.UpgradeProblems) MigrationProblemsContainer(com.liferay.ide.project.core.upgrade.MigrationProblemsContainer) ProblemsContainer(com.liferay.ide.project.core.upgrade.ProblemsContainer)

Example 4 with UpgradeProblems

use of com.liferay.ide.project.core.upgrade.UpgradeProblems in project liferay-ide by liferay.

the class MigrationDecorator method decorate.

@Override
public void decorate(Object element, IDecoration decoration) {
    final List<Problem> problems = new ArrayList<>();
    final List<Problem> resolvedProblems = new ArrayList<>();
    final List<Problem> ignoreProblems = new ArrayList<>();
    if (element instanceof FileProblems) {
        final FileProblems fileProblems = (FileProblems) element;
        problems.addAll(fileProblems.getProblems());
        resolvedProblems.addAll(fileProblems.getProblems(Problem.STATUS_RESOLVED));
        ignoreProblems.addAll(fileProblems.getProblems(Problem.STATUS_IGNORE));
    } else if (element instanceof UpgradeProblems) {
        final UpgradeProblems upgradeProblems = (UpgradeProblems) element;
        for (FileProblems fileProblems : upgradeProblems.getProblems()) {
            problems.addAll(fileProblems.getProblems());
            resolvedProblems.addAll(fileProblems.getProblems(Problem.STATUS_RESOLVED));
            ignoreProblems.addAll(fileProblems.getProblems(Problem.STATUS_IGNORE));
        }
    } else if (element instanceof ProblemsContainer) {
        ProblemsContainer problemsContainer = (ProblemsContainer) element;
        for (UpgradeProblems upgradeProblems : problemsContainer.getProblemsArray()) {
            for (FileProblems fileProblems : upgradeProblems.getProblems()) {
                problems.addAll(fileProblems.getProblems());
                resolvedProblems.addAll(fileProblems.getProblems(Problem.STATUS_RESOLVED));
                ignoreProblems.addAll(fileProblems.getProblems(Problem.STATUS_IGNORE));
            }
        }
    }
    if (ListUtil.isNotEmpty(problems)) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append(problems.size()).append(" total");
        if (ListUtil.isNotEmpty(resolvedProblems)) {
            sb.append(", ");
            sb.append(resolvedProblems.size());
            sb.append(" resolved");
        }
        if (ListUtil.isNotEmpty(ignoreProblems)) {
            sb.append(", ");
            sb.append(ignoreProblems.size());
            sb.append(" ignored");
        }
        sb.append("]");
        decoration.addSuffix(sb.toString());
    }
}
Also used : FileProblems(com.liferay.ide.project.core.upgrade.FileProblems) UpgradeProblems(com.liferay.ide.project.core.upgrade.UpgradeProblems) ArrayList(java.util.ArrayList) Problem(com.liferay.blade.api.Problem) ProblemsContainer(com.liferay.ide.project.core.upgrade.ProblemsContainer)

Aggregations

UpgradeProblems (com.liferay.ide.project.core.upgrade.UpgradeProblems)4 Problem (com.liferay.blade.api.Problem)3 FileProblems (com.liferay.ide.project.core.upgrade.FileProblems)3 ProblemsContainer (com.liferay.ide.project.core.upgrade.ProblemsContainer)3 ArrayList (java.util.ArrayList)3 MigrationProblemsContainer (com.liferay.ide.project.core.upgrade.MigrationProblemsContainer)2 HashSet (java.util.HashSet)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 AutoMigrateException (com.liferay.blade.api.AutoMigrateException)1 AutoMigrator (com.liferay.blade.api.AutoMigrator)1 Migration (com.liferay.blade.api.Migration)1 ProgressMonitor (com.liferay.blade.api.ProgressMonitor)1 CoreUtil (com.liferay.ide.core.util.CoreUtil)1 FileUtil (com.liferay.ide.core.util.FileUtil)1 BreakingChangeSelectedProject (com.liferay.ide.project.core.upgrade.BreakingChangeSelectedProject)1 BreakingChangeSimpleProject (com.liferay.ide.project.core.upgrade.BreakingChangeSimpleProject)1