Search in sources :

Example 11 with RoutineItem

use of org.talend.core.model.properties.RoutineItem in project tdi-studio-se by Talend.

the class ImportItemUtil method checkItem.

private boolean checkItem(ItemRecord itemRecord, boolean overwrite) {
    boolean result = false;
    try {
        Item item = itemRecord.getItem();
        if (item instanceof TDQItem) {
            // hide tdq first
            return false;
        }
        ERepositoryObjectType itemType = ERepositoryObjectType.getItemType(item);
        if (itemType == null) {
            //$NON-NLS-1$
            itemRecord.addError(Messages.getString("ImportItemUtil.unsupportItem"));
            // can't import this item.
            return false;
        }
        cache.initialize(itemType);
        boolean isAllowMultipleName = (itemType == ERepositoryObjectType.SQLPATTERNS || itemType == ERepositoryObjectType.METADATA_FILE_XML);
        String itemPath = null;
        if (item.getState() != null) {
            itemPath = item.getState().getPath();
        } else {
            itemRecord.addError(Messages.getString("ImportItemUtil.unsupportItem"));
            return false;
        }
        boolean nameAvailable = true;
        IRepositoryViewObject itemWithSameId = null;
        IRepositoryViewObject itemWithSameName = null;
        // take care, in cache it's RepositoryViewObject, not RepositoryObject
        for (IRepositoryViewObject current : cache.getItemsFromRepository().get(itemType)) {
            final Property property = itemRecord.getProperty();
            if (property != null) {
                if (property.getLabel() != null && property.getLabel().equalsIgnoreCase(current.getLabel()) && property.getId() != current.getId()) {
                    // repository.
                    if (!isAllowMultipleName || current.getPath().equals(itemPath)) {
                        nameAvailable = false;
                    }
                    // elements
                    if (!nameAvailable) {
                        itemWithSameName = current;
                    }
                }
                if (property.getId() != null && property.getId().equalsIgnoreCase(current.getId())) {
                    itemWithSameId = current;
                }
            }
        }
        itemRecord.setExistingItemWithSameId(itemWithSameId);
        boolean idAvailable = itemWithSameId == null;
        boolean isSystem = false;
        // we do not import built in routines
        if (item.eClass().equals(PropertiesPackage.eINSTANCE.getRoutineItem())) {
            RoutineItem routineItem = (RoutineItem) item;
            if (routineItem.isBuiltIn()) {
                isSystem = true;
            }
        }
        // we do not import system sql patterns
        if (item.eClass().equals(PropertiesPackage.eINSTANCE.getSQLPatternItem())) {
            SQLPatternItem sqlPatternItem = (SQLPatternItem) item;
            if (sqlPatternItem.isSystem()) {
                isSystem = true;
            }
        }
        if (isSystem) {
            itemRecord.addError(Messages.getString("RepositoryUtil.isSystem"));
            return false;
        }
        if (nameAvailable) {
            if (idAvailable) {
                if (!isSystem) {
                    result = true;
                }
            /*
                       * else { itemRecord.addError(Messages.getString("RepositoryUtil.isSystemRoutine")); //$NON-NLS-1$
                       * }
                       */
            } else {
                // same id but different name,no need to care overwrite cause the item will be considered as a
                // different one,see bug 20445
                itemRecord.setState(State.ID_EXISTED);
                // if (overwrite) {
                // result = true;
                // } else {
                // see bug 0005222: [Import items] [Errors and Warnings]
                // id is already in use
                result = true;
            // RepositoryNode nodeWithSameId = RepositoryNodeUtilities.getRepositoryNode(itemWithSameId);
            // IPath path = getPath(nodeWithSameId);
            // itemRecord.addError(Messages.getString(
            //                                "RepositoryUtil.idUsed", itemWithSameId.getLabel(), path.toOSString())); //$NON-NLS-1$
            // }
            }
        } else {
            if (idAvailable) {
                // same name but different id
                itemRecord.setState(State.NAME_EXISTED);
                if (!isSystem && overwrite) {
                    // if anything system, don't replace the source item if same name.
                    // if not from system, can overwrite.
                    itemRecord.setExistingItemWithSameId(itemWithSameName);
                    result = true;
                }
                // if item is locked, cannot overwrite
                if (result && overwrite && itemWithSameName != null) {
                    ERepositoryStatus status = itemWithSameName.getRepositoryStatus();
                    if (status == ERepositoryStatus.LOCK_BY_OTHER || status == ERepositoryStatus.LOCK_BY_USER) {
                        //$NON-NLS-1$
                        itemRecord.addError(Messages.getString("RepositoryUtil.itemLocked"));
                        return false;
                    }
                }
            } else {
                // same name and same id
                itemRecord.setState(State.NAME_AND_ID_EXISTED);
                if (overwrite) {
                    result = true;
                }
                if (!isSystem && overwrite && !itemWithSameName.getProperty().getLabel().equals(itemWithSameId.getProperty().getLabel())) {
                    // if anything system, don't replace the source item if same name.
                    // if not from system, can overwrite.
                    itemRecord.setExistingItemWithSameId(itemWithSameName);
                    result = true;
                }
            }
            if (!result && !isSystem) {
                //$NON-NLS-1$
                itemRecord.addError(Messages.getString("RepositoryUtil.nameUsed"));
            }
        }
        if (result && overwrite && itemRecord.getState() == State.NAME_AND_ID_EXISTED) {
            // if item is locked, cannot overwrite
            if (checkIfLocked(itemRecord)) {
                //$NON-NLS-1$
                itemRecord.addError(Messages.getString("RepositoryUtil.itemLocked"));
                result = false;
            }
        }
    } catch (Exception e) {
        log.error("Error when checking item :" + itemRecord.getPath(), e);
    }
    return result;
}
Also used : ReferenceFileItem(org.talend.core.model.properties.ReferenceFileItem) JobletProcessItem(org.talend.core.model.properties.JobletProcessItem) ConnectionItem(org.talend.core.model.properties.ConnectionItem) SQLPatternItem(org.talend.core.model.properties.SQLPatternItem) ProcessItem(org.talend.core.model.properties.ProcessItem) ContextItem(org.talend.core.model.properties.ContextItem) SnippetItem(org.talend.core.model.properties.SnippetItem) Item(org.talend.core.model.properties.Item) LinkDocumentationItem(org.talend.core.model.properties.LinkDocumentationItem) FolderItem(org.talend.core.model.properties.FolderItem) RoutineItem(org.talend.core.model.properties.RoutineItem) TDQItem(org.talend.core.model.properties.TDQItem) BusinessProcessItem(org.talend.core.model.properties.BusinessProcessItem) FileItem(org.talend.core.model.properties.FileItem) ERepositoryStatus(org.talend.commons.runtime.model.repository.ERepositoryStatus) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) TDQItem(org.talend.core.model.properties.TDQItem) RoutineItem(org.talend.core.model.properties.RoutineItem) SQLPatternItem(org.talend.core.model.properties.SQLPatternItem) ERepositoryObjectType(org.talend.core.model.repository.ERepositoryObjectType) Property(org.talend.core.model.properties.Property) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) PersistenceException(org.talend.commons.exception.PersistenceException)

Example 12 with RoutineItem

use of org.talend.core.model.properties.RoutineItem in project tdi-studio-se by Talend.

the class SaveAsRoutineAction method run.

@Override
public void run() {
    SaveAsRoutineWizard processWizard = new SaveAsRoutineWizard(editorPart);
    WizardDialog dlg = new WizardDialog(Display.getCurrent().getActiveShell(), processWizard);
    if (dlg.open() == Window.OK) {
        try {
            RoutineItem routineItem = processWizard.getRoutineItem();
            // get the IFile
            ICodeGeneratorService service = (ICodeGeneratorService) GlobalServiceRegister.getDefault().getService(ICodeGeneratorService.class);
            ITalendSynchronizer routineSynchronizer = null;
            switch(LanguageManager.getCurrentLanguage()) {
                case JAVA:
                    routineSynchronizer = service.createJavaRoutineSynchronizer();
                    break;
                case PERL:
                    routineSynchronizer = service.createPerlRoutineSynchronizer();
                    break;
                default:
            }
            IFile file = routineSynchronizer.getFile(routineItem);
            if (file == null) {
                return;
            }
            // Set readonly to false since created job will always be editable.
            RoutineEditorInput routineEditorInput = new RoutineEditorInput(file, routineItem);
            IWorkbenchPage page = getActivePage();
            IRepositoryNode repositoryNode = RepositoryNodeUtilities.getRepositoryNode(routineEditorInput.getItem().getProperty().getId(), false);
            routineEditorInput.setRepositoryNode(repositoryNode);
            // here really do the normal save as function
            IDocumentProvider provider = ((AbstractTextEditor) this.editorPart).getDocumentProvider();
            provider.aboutToChange(routineEditorInput);
            provider.saveDocument(null, routineEditorInput, provider.getDocument(this.editorPart.getEditorInput()), true);
            provider.changed(routineEditorInput);
            // copy back from the *.java file to *.item file.
            // @see:StandAloneTalendJavaEditor.doSave(IProgressMonitor monitor)
            ByteArray byteArray = routineItem.getContent();
            byteArray.setInnerContentFromFile(routineEditorInput.getFile());
            IProxyRepositoryFactory repFactory = DesignerPlugin.getDefault().getRepositoryService().getProxyRepositoryFactory();
            repFactory.save(routineItem);
            // close the old editor
            page.closeEditor(this.editorPart, false);
            // open the new editor, because at the same time, there will update the jobSetting/componentSetting view
            switch(LanguageManager.getCurrentLanguage()) {
                case JAVA:
                    page.openEditor(routineEditorInput, StandAloneTalendJavaEditor.ID, true);
                    break;
                default:
            }
        } catch (Exception e) {
            MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error", "Routine could not be saved" + " : " + e.getMessage());
            ExceptionHandler.process(e);
        }
    }
}
Also used : ITalendSynchronizer(org.talend.designer.codegen.ITalendSynchronizer) IFile(org.eclipse.core.resources.IFile) IRepositoryNode(org.talend.repository.model.IRepositoryNode) RoutineItem(org.talend.core.model.properties.RoutineItem) RoutineEditorInput(org.talend.repository.ui.actions.routines.RoutineEditorInput) ICodeGeneratorService(org.talend.designer.codegen.ICodeGeneratorService) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ByteArray(org.talend.core.model.properties.ByteArray) WizardDialog(org.eclipse.jface.wizard.WizardDialog) SaveAsRoutineWizard(org.talend.designer.core.ui.wizards.SaveAsRoutineWizard) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory)

Example 13 with RoutineItem

use of org.talend.core.model.properties.RoutineItem in project tdi-studio-se by Talend.

the class WSDL2JAVAController method createRoutine.

/**
     * DOC xtan Comment method "createRoutine".
     * 
     * @param path
     */
private RoutineItem createRoutine(final IPath path, String label, File initFile, String name) {
    Property property = PropertiesFactory.eINSTANCE.createProperty();
    property.setAuthor(((RepositoryContext) CorePlugin.getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getUser());
    property.setVersion(VersionUtils.DEFAULT_VERSION);
    //$NON-NLS-1$
    property.setStatusCode("");
    property.setLabel(label);
    final RoutineItem routineItem = PropertiesFactory.eINSTANCE.createRoutineItem();
    routineItem.setProperty(property);
    ByteArray byteArray = PropertiesFactory.eINSTANCE.createByteArray();
    InputStream stream = null;
    try {
        stream = initFile.toURL().openStream();
        byte[] innerContent = new byte[stream.available()];
        stream.read(innerContent);
        byteArray.setInnerContent(innerContent);
    } catch (IOException e) {
        ExceptionHandler.process(e);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            //
            }
        }
    }
    String routineContent = new String(byteArray.getInnerContent());
    routineContent = chanageRoutinesPackage(routineContent, name);
    byteArray.setInnerContent(routineContent.getBytes());
    routineItem.setContent(byteArray);
    final IProxyRepositoryFactory repositoryFactory = ProxyRepositoryFactory.getInstance();
    try {
        property.setId(repositoryFactory.getNextId());
        repositoryFactory.createParentFoldersRecursively(ERepositoryObjectType.getItemType(routineItem), path);
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                try {
                    repositoryFactory.create(routineItem, path);
                } catch (PersistenceException e) {
                    ExceptionHandler.process(e);
                }
            }
        });
    } catch (PersistenceException e) {
        ExceptionHandler.process(e);
    }
    if (routineItem.eResource() != null) {
        addWsdlNeedLib(routineItem);
    }
    return routineItem;
}
Also used : InputStream(java.io.InputStream) PersistenceException(org.talend.commons.exception.PersistenceException) ByteArray(org.talend.core.model.properties.ByteArray) RoutineItem(org.talend.core.model.properties.RoutineItem) IOException(java.io.IOException) IDynamicProperty(org.talend.core.ui.properties.tab.IDynamicProperty) Property(org.talend.core.model.properties.Property) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory)

Example 14 with RoutineItem

use of org.talend.core.model.properties.RoutineItem in project tdi-studio-se by Talend.

the class WSDL2JAVAController method generateJavaFile.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.talend.designer.core.ui.editor.properties2.editors.AbstractElementPropertySectionController#createCommand()
     */
private void generateJavaFile() {
    final IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
    RepositoryWorkUnit<Object> workUnit = new //$NON-NLS-1$
    RepositoryWorkUnit<Object>(//$NON-NLS-1$
    "", //$NON-NLS-1$
    this) {

        @Override
        protected void run() throws PersistenceException {
            Node node = (Node) elem;
            IProcess process = node.getProcess();
            String jobName = process.getName();
            String nodeName = node.getUniqueName();
            //$NON-NLS-1$
            String wsdlfile = (String) node.getPropertyValue("ENDPOINT");
            wsdlfile = wsdlfile.substring(1, wsdlfile.length() - 1);
            if (wsdlfile.equals("")) {
                Display.getDefault().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        MessageDialog.openError(Display.getDefault().getActiveShell(), org.talend.designer.core.i18n.Messages.getString("WSDL2JAVAController.TOS"), org.talend.designer.core.i18n.Messages.getString("WSDL2JAVAController.WSDLEquals"));
                    }
                });
                return;
            }
            File dir = new File(getTmpFolder());
            final TalendWSDL2Java java2WSDL = new TalendWSDL2Java();
            //$NON-NLS-1$ //$NON-NLS-2$
            boolean hasError = java2WSDL.generateWSDL(new String[] { "-o" + dir, "-p" + PACK, wsdlfile });
            // give some info about the generate stub.jar result to GUI.
            final String tempWsdlfile = wsdlfile;
            if (hasError) {
                Display.getDefault().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        MessageDialog.openError(Display.getDefault().getActiveShell(), org.talend.designer.core.i18n.Messages.getString("WSDL2JAVAController.TOS"), //$NON-NLS-1$ //$NON-NLS-2$
                        org.talend.designer.core.i18n.Messages.getString(//$NON-NLS-1$ //$NON-NLS-2$
                        "WSDL2JAVAController.generateFileFailed", java2WSDL.getException().getClass().getCanonicalName(), java2WSDL.getException().getMessage()));
                    }
                });
            } else {
                Display.getDefault().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        MessageDialog.openInformation(Display.getDefault().getActiveShell(), //$NON-NLS-1$
                        org.talend.designer.core.i18n.Messages.getString("WSDL2JAVAController.TOS"), org.talend.designer.core.i18n.Messages.getString("WSDL2JAVAController.generateFileFailedFromWSDL", //$NON-NLS-1$
                        tempWsdlfile));
                    }
                });
            }
            //$NON-NLS-1$
            IPath path = new Path(jobName + "/" + nodeName);
            //$NON-NLS-1$
            String[] filter = new String[] { "java" };
            Collection listFiles = FileUtils.listFiles(dir, filter, true);
            Iterator iterator = listFiles.iterator();
            String name = "";
            for (int i = 0; i < listFiles.size(); i++) {
                File javaFile = (File) listFiles.toArray()[i];
                String parentFileName = javaFile.getParentFile().getName();
                if (!parentFileName.equals("routines")) {
                    name = parentFileName;
                }
            }
            List<RoutineItem> returnItemList = new ArrayList<RoutineItem>();
            while (iterator.hasNext()) {
                File javaFile = (File) iterator.next();
                String fileName = javaFile.getName();
                String label = fileName.substring(0, fileName.indexOf('.'));
                try {
                    RoutineItem returnItem = createRoutine(path, label, javaFile, name);
                    returnItemList.add(returnItem);
                    syncRoutine(returnItem, true, name);
                    refreshProject();
                } catch (IllegalArgumentException e) {
                // nothing need to do for the duplicate label, there don't overwrite it.
                } catch (Exception e) {
                    ExceptionHandler.process(e);
                }
            }
            Project currentProject = ProjectManager.getInstance().getCurrentProject();
            IRepositoryService service = (IRepositoryService) GlobalServiceRegister.getDefault().getService(IRepositoryService.class);
            IProxyRepositoryFactory factory = service.getProxyRepositoryFactory();
            List<IRepositoryViewObject> all;
            Item item = null;
            try {
                all = factory.getAll(currentProject, ERepositoryObjectType.PROCESS, true, true);
                for (IRepositoryViewObject repositoryViewObject : all) {
                    if (repositoryViewObject.getLabel().equals(jobName)) {
                        item = repositoryViewObject.getProperty().getItem();
                    }
                }
            } catch (PersistenceException ex) {
                ExceptionHandler.process(ex);
            }
            try {
                List<RoutinesParameterType> needList = new ArrayList<RoutinesParameterType>();
                List<RoutinesParameterType> createJobRoutineDependencies = RoutinesUtil.createJobRoutineDependencies(false);
                for (RoutineItem returnItem : returnItemList) {
                    for (RoutinesParameterType routinesParameterType : createJobRoutineDependencies) {
                        if (routinesParameterType.getId().equals(returnItem.getProperty().getId())) {
                            needList.add(routinesParameterType);
                        }
                    }
                }
                if (process instanceof org.talend.designer.core.ui.editor.process.Process) {
                    ((org.talend.designer.core.ui.editor.process.Process) process).addGeneratingRoutines(needList);
                }
            } catch (PersistenceException e) {
                ExceptionHandler.process(e);
            }
            // try {
            // RoutinesUtil.createJobRoutineDependencies(false);
            // } catch (PersistenceException e) {
            // ExceptionHandler.process(e);
            // }
            FilesUtils.removeFolder(dir, true);
        }
    };
    workUnit.setAvoidUnloadResources(true);
    factory.executeRepositoryWorkUnit(workUnit);
}
Also used : RoutinesParameterType(org.talend.designer.core.model.utils.emf.talendfile.RoutinesParameterType) Node(org.talend.designer.core.ui.editor.nodes.Node) ArrayList(java.util.ArrayList) RepositoryWorkUnit(org.talend.repository.RepositoryWorkUnit) IProcess(org.talend.core.model.process.IProcess) IRepositoryService(org.talend.repository.model.IRepositoryService) Item(org.talend.core.model.properties.Item) RoutineItem(org.talend.core.model.properties.RoutineItem) Iterator(java.util.Iterator) IProcess(org.talend.core.model.process.IProcess) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) RoutineItem(org.talend.core.model.properties.RoutineItem) Point(org.eclipse.swt.graphics.Point) CoreException(org.eclipse.core.runtime.CoreException) SystemException(org.talend.commons.exception.SystemException) IOException(java.io.IOException) PersistenceException(org.talend.commons.exception.PersistenceException) Project(org.talend.core.model.general.Project) ITalendProcessJavaProject(org.talend.core.runtime.process.ITalendProcessJavaProject) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) PersistenceException(org.talend.commons.exception.PersistenceException) Collection(java.util.Collection) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 15 with RoutineItem

use of org.talend.core.model.properties.RoutineItem in project tdi-studio-se by Talend.

the class RemoveRoutineAntlrJarDependencyTask method execute.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.core.model.migration.AbstractItemMigrationTask#execute(org.talend.core.model.properties.Item)
     */
@Override
public ExecutionResult execute(Item item) {
    if (getProject().getLanguage() == ECodeLanguage.JAVA) {
        RoutineItem routineItem = (RoutineItem) item;
        if (routineItem.isBuiltIn()) {
            return ExecutionResult.NOTHING_TO_DO;
        }
        try {
            EList<IMPORTType> imports = routineItem.getImports();
            List<IMPORTType> toRemove = new ArrayList<IMPORTType>();
            for (IMPORTType importElement : imports) {
                if ("antlr-3.3.jar".equals(importElement.getMODULE())) {
                    //$NON-NLS-1$
                    toRemove.add(importElement);
                }
            }
            if (toRemove.size() > 0) {
                imports.removeAll(toRemove);
                ProxyRepositoryFactory.getInstance().save(routineItem);
            }
            return ExecutionResult.SUCCESS_NO_ALERT;
        } catch (Exception e) {
            ExceptionHandler.process(e);
            return ExecutionResult.FAILURE;
        }
    } else {
        return ExecutionResult.NOTHING_TO_DO;
    }
}
Also used : ArrayList(java.util.ArrayList) IMPORTType(org.talend.designer.core.model.utils.emf.component.IMPORTType) RoutineItem(org.talend.core.model.properties.RoutineItem)

Aggregations

RoutineItem (org.talend.core.model.properties.RoutineItem)39 PersistenceException (org.talend.commons.exception.PersistenceException)15 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)15 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)13 Item (org.talend.core.model.properties.Item)12 Property (org.talend.core.model.properties.Property)12 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)9 CoreException (org.eclipse.core.runtime.CoreException)8 ProcessItem (org.talend.core.model.properties.ProcessItem)8 SQLPatternItem (org.talend.core.model.properties.SQLPatternItem)8 File (java.io.File)7 IFile (org.eclipse.core.resources.IFile)7 IMPORTType (org.talend.designer.core.model.utils.emf.component.IMPORTType)7 Project (org.talend.core.model.general.Project)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 HashSet (java.util.HashSet)5 PartInitException (org.eclipse.ui.PartInitException)5 SystemException (org.talend.commons.exception.SystemException)5 List (java.util.List)4