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;
}
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);
}
}
}
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;
}
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);
}
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;
}
}
Aggregations