Search in sources :

Example 1 with AutoMigrateException

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

the class ImportStatementMigrator method correctProblems.

@Override
public int correctProblems(File file, List<Problem> problems) throws AutoMigrateException {
    int problemsFixed = 0;
    List<String> importsToRewrite = new ArrayList<>();
    for (Problem problem : problems) {
        boolean problemFound = false;
        if (problem.autoCorrectContext instanceof String) {
            String importData = problem.autoCorrectContext;
            if ((importData != null) && importData.startsWith(_PREFIX)) {
                String importValue = importData.substring(_PREFIX.length());
                if (_importFixes.containsKey(importValue)) {
                    importsToRewrite.add(problem.getLineNumber() + "," + importValue);
                    problemFound = true;
                }
            }
        }
        if (problemFound) {
            problemsFixed++;
        }
    }
    if (ListUtil.isNotEmpty(importsToRewrite)) {
        try (InputStream inputStream = Files.newInputStream(file.toPath())) {
            String[] lines = _readLines(inputStream);
            String[] editedLines = new String[lines.length];
            System.arraycopy(lines, 0, editedLines, 0, lines.length);
            for (String importData : importsToRewrite) {
                String[] importMap = importData.split(",");
                int lineNumber = Integer.parseInt(importMap[0]);
                String importName = importMap[1];
                editedLines[lineNumber - 1] = editedLines[lineNumber - 1].replaceAll(importName, _importFixes.get(importName));
            }
            StringBuilder sb = new StringBuilder();
            for (String editedLine : editedLines) {
                sb.append(editedLine);
                sb.append(System.getProperty("line.separator"));
            }
            FileWriter writer = new FileWriter(file);
            writer.write(sb.toString());
            writer.close();
            _clearCache(file);
            return problemsFixed;
        } catch (IOException ioe) {
            throw new AutoMigrateException("Unable to auto-correct", ioe);
        }
    }
    return 0;
}
Also used : AutoMigrateException(com.liferay.blade.api.AutoMigrateException) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Problem(com.liferay.blade.api.Problem) IOException(java.io.IOException)

Example 2 with AutoMigrateException

use of com.liferay.blade.api.AutoMigrateException 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 3 with AutoMigrateException

use of com.liferay.blade.api.AutoMigrateException 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 4 with AutoMigrateException

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

the class JSPTagMigrator method correctProblems.

@Override
public int correctProblems(File file, List<Problem> problems) throws AutoMigrateException {
    int corrected = 0;
    List<Integer> autoCorrectTagOffsets = new ArrayList<>();
    Stream<Problem> stream = problems.stream();
    Class<? extends JSPTagMigrator> class1 = getClass();
    String autoCorrectContext = "jsptag:" + class1.getName();
    stream.filter(p -> p.autoCorrectContext.equals(autoCorrectContext)).map(p -> p.getStartOffset()).sorted();
    for (Problem problem : problems) {
        if ((problem.autoCorrectContext != null) && problem.autoCorrectContext.equals("jsptag:" + class1.getName())) {
            autoCorrectTagOffsets.add(problem.getStartOffset());
        }
    }
    Collections.sort(autoCorrectTagOffsets, new Comparator<Integer>() {

        @Override
        public int compare(Integer i1, Integer i2) {
            return i2.compareTo(i1);
        }
    });
    IFile jspFile = getJSPFile(file);
    if (ListUtil.isNotEmpty(autoCorrectTagOffsets)) {
        IDOMModel domModel = null;
        try {
            domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(jspFile);
            List<IDOMElement> elementsToCorrect = new ArrayList<>();
            for (int startOffset : autoCorrectTagOffsets) {
                IndexedRegion region = domModel.getIndexedRegion(startOffset);
                if (region instanceof IDOMElement) {
                    IDOMElement element = (IDOMElement) region;
                    elementsToCorrect.add(element);
                }
            }
            for (IDOMElement element : elementsToCorrect) {
                domModel.aboutToChangeModel();
                if (_newAttrValues.length == 1) {
                    element.setAttribute(_attrNames[0], _newAttrValues[0]);
                    corrected++;
                } else if (_newAttrNames.length == 1) {
                    String value = element.getAttribute(_attrNames[0]);
                    element.removeAttribute(_attrNames[0]);
                    element.setAttribute(_newAttrNames[0], value);
                    corrected++;
                } else if (ListUtil.isNotEmpty(_newTagNames)) {
                    String tagName = element.getTagName();
                    NamedNodeMap attributes = element.getAttributes();
                    NodeList childNodes = element.getChildNodes();
                    String nodeValue = element.getNodeValue();
                    String newTagName = "";
                    for (int i = 0; i < _tagNames.length; i++) {
                        if (_tagNames[i].equals(tagName)) {
                            newTagName = _newTagNames[i];
                            break;
                        }
                    }
                    if (newTagName.equals("")) {
                        continue;
                    }
                    Element newNode = element.getOwnerDocument().createElement(newTagName);
                    if (nodeValue != null) {
                        newNode.setNodeValue(nodeValue);
                    }
                    for (int i = 0; i < attributes.getLength(); i++) {
                        Node attribute = attributes.item(i);
                        newNode.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
                    }
                    for (int i = 0; i < childNodes.getLength(); i++) {
                        Node childNode = childNodes.item(i);
                        newNode.appendChild(childNode.cloneNode(true));
                    }
                    element.getParentNode().replaceChild(newNode, element);
                    corrected++;
                }
                domModel.changedModel();
                domModel.save();
            }
        } catch (Exception e) {
            throw new AutoMigrateException("Unable to auto-correct", e);
        } finally {
            if (domModel != null) {
                domModel.releaseFromEdit();
            }
        }
        IPath location = jspFile.getLocation();
        if ((corrected > 0) && !location.toFile().equals(file)) {
            try (InputStream jspFileContent = jspFile.getContents()) {
                Files.copy(jspFileContent, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
            } catch (Exception e) {
                throw new AutoMigrateException("Error writing corrected file.", e);
            }
        }
    }
    return corrected;
}
Also used : SearchResult(com.liferay.blade.api.SearchResult) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) IPath(org.eclipse.core.runtime.IPath) Node(org.w3c.dom.Node) ListUtil(com.liferay.ide.core.util.ListUtil) IFile(org.eclipse.core.resources.IFile) NamedNodeMap(org.w3c.dom.NamedNodeMap) JSPFile(com.liferay.blade.api.JSPFile) NodeList(org.w3c.dom.NodeList) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Files(java.nio.file.Files) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) File(java.io.File) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) Problem(com.liferay.blade.api.Problem) List(java.util.List) Stream(java.util.stream.Stream) Element(org.w3c.dom.Element) AutoMigrator(com.liferay.blade.api.AutoMigrator) StructuredModelManager(org.eclipse.wst.sse.core.StructuredModelManager) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) IFile(org.eclipse.core.resources.IFile) NamedNodeMap(org.w3c.dom.NamedNodeMap) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) IPath(org.eclipse.core.runtime.IPath) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) Problem(com.liferay.blade.api.Problem)

Example 5 with AutoMigrateException

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

the class MVCPortletClassInPortletXML method correctProblems.

@Override
public int correctProblems(File file, List<Problem> problems) throws AutoMigrateException {
    int corrected = 0;
    IFile xmlFile = getXmlFile(file);
    IDOMModel xmlModel = null;
    if (xmlFile != null) {
        try {
            xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(xmlFile);
            List<IDOMElement> elementsToCorrect = new ArrayList<>();
            for (Problem problem : problems) {
                if (_KEY.equals(problem.autoCorrectContext)) {
                    IndexedRegion region = xmlModel.getIndexedRegion(problem.startOffset);
                    if (region instanceof IDOMElement) {
                        IDOMElement element = (IDOMElement) region;
                        elementsToCorrect.add(element);
                    }
                }
            }
            for (IDOMElement element : elementsToCorrect) {
                xmlModel.aboutToChangeModel();
                _removeChildren(element);
                Text textContent = element.getOwnerDocument().createTextNode("com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet");
                element.appendChild(textContent);
                xmlModel.changedModel();
                corrected++;
            }
            xmlModel.save();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlModel != null) {
                xmlModel.releaseFromEdit();
            }
        }
    }
    IPath location = xmlFile.getLocation();
    if ((corrected > 0) && !location.toFile().equals(file)) {
        try (InputStream xmlFileContent = xmlFile.getContents()) {
            Files.copy(xmlFileContent, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } catch (Exception e) {
            throw new AutoMigrateException("Error writing corrected file.", e);
        }
    }
    return corrected;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Text(org.w3c.dom.Text) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) Problem(com.liferay.blade.api.Problem)

Aggregations

AutoMigrateException (com.liferay.blade.api.AutoMigrateException)5 Problem (com.liferay.blade.api.Problem)5 ArrayList (java.util.ArrayList)4 AutoMigrator (com.liferay.blade.api.AutoMigrator)3 InputStream (java.io.InputStream)3 IOException (java.io.IOException)2 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)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 IPath (org.eclipse.core.runtime.IPath)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)2 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)2 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)2 BundleContext (org.osgi.framework.BundleContext)2