Search in sources :

Example 6 with XmiResourceManager

use of org.talend.core.repository.utils.XmiResourceManager in project tdi-studio-se by Talend.

the class AddContextCommentValueMigrationTaskTest method createTempProject.

private static void createTempProject() throws CoreException, PersistenceException, LoginException {
    Project projectInfor = new Project();
    projectInfor.setLabel("testauto");
    projectInfor.setDescription("no desc");
    projectInfor.setLanguage(ECodeLanguage.JAVA);
    User user = PropertiesFactory.eINSTANCE.createUser();
    user.setLogin("testauto@talend.com");
    projectInfor.setAuthor(user);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    String technicalLabel = Project.createTechnicalName(projectInfor.getLabel());
    IProject prj = root.getProject(technicalLabel);
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    try {
        IProjectDescription desc = null;
        if (prj.exists()) {
            // always delete to avoid conflicts between 2 tests
            prj.delete(true, null);
        }
        desc = workspace.newProjectDescription(technicalLabel);
        desc.setNatureIds(new String[] { TalendNature.ID });
        desc.setComment(projectInfor.getDescription());
        prj.create(desc, null);
        prj.open(IResource.DEPTH_INFINITE, null);
        prj.setDefaultCharset("UTF-8", null);
    } catch (CoreException e) {
        throw new PersistenceException(e);
    }
    sampleProject = new Project();
    // Fill project object
    sampleProject.setLabel(projectInfor.getLabel());
    sampleProject.setDescription(projectInfor.getDescription());
    sampleProject.setLanguage(projectInfor.getLanguage());
    sampleProject.setAuthor(projectInfor.getAuthor());
    sampleProject.setLocal(true);
    sampleProject.setTechnicalLabel(technicalLabel);
    XmiResourceManager xmiResourceManager = new XmiResourceManager();
    Resource projectResource = xmiResourceManager.createProjectResource(prj);
    projectResource.getContents().add(sampleProject.getEmfProject());
    projectResource.getContents().add(sampleProject.getAuthor());
    xmiResourceManager.saveResource(projectResource);
}
Also used : IProject(org.eclipse.core.resources.IProject) Project(org.talend.core.model.general.Project) User(org.talend.core.model.properties.User) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) PersistenceException(org.talend.commons.exception.PersistenceException) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) XmiResourceManager(org.talend.core.repository.utils.XmiResourceManager) IProject(org.eclipse.core.resources.IProject)

Example 7 with XmiResourceManager

use of org.talend.core.repository.utils.XmiResourceManager in project tdi-studio-se by Talend.

the class FixUnevenItemContextParametersMigrationTaskTest method createTempProject.

private static void createTempProject() throws CoreException, PersistenceException, LoginException {
    Project projectInfor = new Project();
    projectInfor.setLabel("testauto");
    projectInfor.setDescription("no desc");
    projectInfor.setLanguage(ECodeLanguage.JAVA);
    User user = PropertiesFactory.eINSTANCE.createUser();
    user.setLogin("testauto@talend.com");
    projectInfor.setAuthor(user);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    String technicalLabel = Project.createTechnicalName(projectInfor.getLabel());
    IProject prj = root.getProject(technicalLabel);
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    try {
        IProjectDescription desc = null;
        if (prj.exists()) {
            // always delete to avoid conflicts between 2 tests
            prj.delete(true, null);
        }
        desc = workspace.newProjectDescription(technicalLabel);
        desc.setNatureIds(new String[] { TalendNature.ID });
        desc.setComment(projectInfor.getDescription());
        prj.create(desc, null);
        prj.open(IResource.DEPTH_INFINITE, null);
        prj.setDefaultCharset("UTF-8", null);
    } catch (CoreException e) {
        throw new PersistenceException(e);
    }
    sampleProject = new Project();
    // Fill project object
    sampleProject.setLabel(projectInfor.getLabel());
    sampleProject.setDescription(projectInfor.getDescription());
    sampleProject.setLanguage(projectInfor.getLanguage());
    sampleProject.setAuthor(projectInfor.getAuthor());
    sampleProject.setLocal(true);
    sampleProject.setTechnicalLabel(technicalLabel);
    XmiResourceManager xmiResourceManager = new XmiResourceManager();
    Resource projectResource = xmiResourceManager.createProjectResource(prj);
    projectResource.getContents().add(sampleProject.getEmfProject());
    projectResource.getContents().add(sampleProject.getAuthor());
    xmiResourceManager.saveResource(projectResource);
}
Also used : IProject(org.eclipse.core.resources.IProject) Project(org.talend.core.model.general.Project) User(org.talend.core.model.properties.User) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) PersistenceException(org.talend.commons.exception.PersistenceException) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) XmiResourceManager(org.talend.core.repository.utils.XmiResourceManager) IProject(org.eclipse.core.resources.IProject)

Example 8 with XmiResourceManager

use of org.talend.core.repository.utils.XmiResourceManager in project tdi-studio-se by Talend.

the class ChangeXmiSerialization method execute.

@Override
public ExecutionResult execute(Project project) {
    try {
        if (!project.isLocal()) {
            return ExecutionResult.NOTHING_TO_DO;
        }
        IProject iProject = ResourceUtils.getProject(project);
        xmiResourceManager = new XmiResourceManager();
        xmiResourceManager.setUseOldProjectFile(true);
        if (!xmiResourceManager.hasTalendProjectFile(iProject)) {
            return ExecutionResult.NOTHING_TO_DO;
        }
        PropertiesResourcesCollector propertiesResourcesCollector = new PropertiesResourcesCollector();
        iProject.accept(propertiesResourcesCollector);
        Collection<IFile> propertiesResourcesFiles = propertiesResourcesCollector.getPropertiesResourcesFiles();
        for (IFile file : propertiesResourcesFiles) {
            Property property = xmiResourceManager.loadProperty(file);
            // we access the author (resolve proxy)
            boolean correctAuthor = false;
            try {
                property.getAuthor();
            } catch (Exception e) {
                // author is null : fix old bug
                correctAuthor = true;
            }
            if (correctAuthor || property.getAuthor().getLogin() == null) {
                property.setAuthor(project.getAuthor());
            }
            EcoreUtil.resolveAll(property.eResource());
            // if .properties is saved then migrated .item must also be saved
            if (property.getItem() instanceof BusinessProcessItem) {
                BusinessProcessItem businessProcessItem = (BusinessProcessItem) property.getItem();
                businessProcessItem.getNotation();
            }
            modifiedResources.add(property.eResource());
        }
        Resource projectResource = xmiResourceManager.loadProject(iProject).eResource();
        xmiResourceManager.setUseOldProjectFile(false);
        Resource newProjectResource = xmiResourceManager.createProjectResource(iProject);
        EObject[] objects = (EObject[]) projectResource.getContents().toArray();
        for (EObject object : objects) {
            newProjectResource.getContents().add(object);
        }
        modifiedResources.add(newProjectResource);
        for (Resource resource : modifiedResources) {
            try {
                resource.setModified(true);
                xmiResourceManager.saveResource(resource);
            } catch (PersistenceException e) {
                // e.printStackTrace();
                ExceptionHandler.process(e);
            }
        }
        xmiResourceManager.deleteResource(projectResource);
        return ExecutionResult.SUCCESS_NO_ALERT;
    } catch (Exception e) {
        ExceptionHandler.process(e);
        return ExecutionResult.FAILURE;
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) BusinessProcessItem(org.talend.core.model.properties.BusinessProcessItem) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) XmiResourceManager(org.talend.core.repository.utils.XmiResourceManager) IProject(org.eclipse.core.resources.IProject) PersistenceException(org.talend.commons.exception.PersistenceException) EObject(org.eclipse.emf.ecore.EObject) PersistenceException(org.talend.commons.exception.PersistenceException) Property(org.talend.core.model.properties.Property)

Example 9 with XmiResourceManager

use of org.talend.core.repository.utils.XmiResourceManager in project tdi-studio-se by Talend.

the class FixProjectResourceLink method execute.

@Override
public ExecutionResult execute(Item item) {
    if (item.getProperty().getAuthor() == null) {
        // this case shouldn't happen normally, but just in case, each item should have one author
        IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
        item.getProperty().setAuthor(factory.getRepositoryContext().getUser());
    }
    if (ERepositoryObjectType.getItemType(item).isDQItemType() && !ERepositoryObjectType.getItemType(item).isDIItemType()) {
        return ExecutionResult.NOTHING_TO_DO;
    }
    if (item.getProperty().getAuthor().getLogin() != null) {
        return ExecutionResult.NOTHING_TO_DO;
    }
    // if go here, it means property got wrong link to the project.
    // (like for example item look for: ../../../talend.project, but it should be: ../../talend.project)
    IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
    item.getProperty().setAuthor(factory.getRepositoryContext().getUser());
    clearReferenceResourcesFileName(item);
    // now we should remove wrong URI from resource manager.
    // if this bug above happen, means the resourceSet keep wrong link to the project
    XmiResourceManager manager = ProxyRepositoryFactory.getInstance().getRepositoryFactoryFromProvider().getResourceManager();
    List<Resource> toDelete = new ArrayList<Resource>();
    for (Resource resource : manager.resourceSet.getResources()) {
        if (resource.getURI().toString().contains(manager.getProjectFilename())) {
            try {
                IFile file = URIHelper.getFile(resource.getURI());
                if (!file.exists()) {
                    toDelete.add(resource);
                }
            } catch (Exception e) {
                toDelete.add(resource);
            }
        }
    }
    EmfHelper.visitChilds(item);
    try {
        factory.save(item, true);
    } catch (PersistenceException e) {
        ExceptionHandler.process(e);
    } catch (UnsupportedOperationException e1) {
        ExceptionHandler.process(e1);
    }
    manager.resourceSet.getResources().removeAll(toDelete);
    return ExecutionResult.SUCCESS_NO_ALERT;
}
Also used : IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) Resource(org.eclipse.emf.ecore.resource.Resource) PersistenceException(org.talend.commons.exception.PersistenceException) XmiResourceManager(org.talend.core.repository.utils.XmiResourceManager) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) PersistenceException(org.talend.commons.exception.PersistenceException)

Example 10 with XmiResourceManager

use of org.talend.core.repository.utils.XmiResourceManager in project tdi-studio-se by Talend.

the class GenericRepositoryContentHandler method save.

private Resource save(GenericConnectionItem item) {
    XmiResourceManager xmiResourceManager = ProxyRepositoryFactory.getInstance().getRepositoryFactoryFromProvider().getResourceManager();
    Resource itemResource = xmiResourceManager.getItemResource(item);
    itemResource.getContents().clear();
    MetadataManager.addContents(item, itemResource);
    return itemResource;
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) XmiResourceManager(org.talend.core.repository.utils.XmiResourceManager)

Aggregations

XmiResourceManager (org.talend.core.repository.utils.XmiResourceManager)11 PersistenceException (org.talend.commons.exception.PersistenceException)9 IProject (org.eclipse.core.resources.IProject)8 Resource (org.eclipse.emf.ecore.resource.Resource)8 IResource (org.eclipse.core.resources.IResource)7 IWorkspace (org.eclipse.core.resources.IWorkspace)5 CoreException (org.eclipse.core.runtime.CoreException)4 IFile (org.eclipse.core.resources.IFile)3 IProjectDescription (org.eclipse.core.resources.IProjectDescription)3 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)3 IPath (org.eclipse.core.runtime.IPath)3 Path (org.eclipse.core.runtime.Path)3 Project (org.talend.core.model.general.Project)3 Project (org.talend.core.model.properties.Project)3 Property (org.talend.core.model.properties.Property)3 User (org.talend.core.model.properties.User)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Item (org.talend.core.model.properties.Item)2