use of org.talend.core.model.properties.helper.ByteArrayResource in project tdi-studio-se by Talend.
the class ImportItemUtil method importItemRecord.
private void importItemRecord(ResourcesManager manager, ItemRecord itemRecord, boolean overwrite, IPath destinationPath, final Set<String> overwriteDeletedItems, final Set<String> idDeletedBeforeImport, String contentType, final IProgressMonitor monitor) {
//$NON-NLS-1$
monitor.subTask(Messages.getString("ImportItemWizardPage.Importing") + itemRecord.getItemName());
resolveItem(manager, itemRecord);
if (!itemRecord.isValid()) {
return;
}
int num = 0;
for (Object obj : itemRecord.getResourceSet().getResources()) {
if (!(obj instanceof PropertiesProjectResourceImpl)) {
if (obj instanceof XMIResourceImpl) {
num++;
if (num > 2) {
// 2 so that metadata migration for 4.1 works
try {
throw new InvocationTargetException(new PersistenceException("The source file of " + itemRecord.getLabel() + " has error,Please check it!"));
} catch (InvocationTargetException e) {
ExceptionHandler.process(e);
}
return;
}
}
}
}
final Item item = itemRecord.getItem();
if (item != null) {
ProxyRepositoryFactory repFactory = ProxyRepositoryFactory.getInstance();
ERepositoryObjectType itemType = ERepositoryObjectType.getItemType(item);
IPath path = new Path(item.getState().getPath());
if (destinationPath != null && itemType.name().equals(contentType)) {
path = destinationPath.append(path);
}
try {
FolderItem folderItem = repFactory.getFolderItem(ProjectManager.getInstance().getCurrentProject(), itemType, path);
if (folderItem == null) {
// if this folder does not exists (and it's parents), it will check if the folder was originally
// deleted in source project.
// if yes, it will set back the delete status to the folder, to keep the same as the original
// project when import.
// Without this code, deleted folders of items imported will not be in the recycle bin after import.
// delete status is set finally in the function checkDeletedFolders
IPath curPath = path;
EList deletedFoldersFromOriginalProject = itemRecord.getItemProject().getDeletedFolders();
while (folderItem == null && !curPath.isEmpty() && !curPath.isRoot()) {
if (deletedFoldersFromOriginalProject.contains(new Path(itemType.getFolder()).append(curPath.toPortableString()).toPortableString())) {
if (!foldersCreated.containsKey(itemType)) {
foldersCreated.put(itemType, new HashSet<String>());
}
foldersCreated.get(itemType).add(curPath.toPortableString());
}
if (curPath.segments().length > 0) {
curPath = curPath.removeLastSegments(1);
folderItem = repFactory.getFolderItem(ProjectManager.getInstance().getCurrentProject(), itemType, curPath);
}
}
}
repFactory.createParentFoldersRecursively(ProjectManager.getInstance().getCurrentProject(), itemType, path, true);
} catch (Exception e) {
logError(e);
//$NON-NLS-1$
path = new Path("");
}
try {
Item tmpItem = item;
// delete existing items before importing, this should be done
// once for a different id
String id = itemRecord.getProperty().getId();
IRepositoryViewObject lastVersion = itemRecord.getExistingItemWithSameId();
if (lastVersion != null && overwrite && !itemRecord.isLocked() && (itemRecord.getState() == State.ID_EXISTED || itemRecord.getState() == State.NAME_EXISTED || itemRecord.getState() == State.NAME_AND_ID_EXISTED) && !deletedItems.contains(id)) {
if (!overwriteDeletedItems.contains(id)) {
// bug 10520.
ERepositoryStatus status = repFactory.getStatus(lastVersion);
if (status == ERepositoryStatus.DELETED) {
// restore first.
repFactory.restoreObject(lastVersion, path);
}
overwriteDeletedItems.add(id);
}
/* only delete when name exsit rather than id exist */
if (itemRecord.getState().equals(ItemRecord.State.NAME_EXISTED) || itemRecord.getState().equals(ItemRecord.State.NAME_AND_ID_EXISTED)) {
if (!idDeletedBeforeImport.contains(id)) {
// TDI-19535 (check if exists, delete all items with same id)
List<IRepositoryViewObject> allVersionToDelete = repFactory.getAllVersion(ProjectManager.getInstance().getCurrentProject(), lastVersion.getId(), false);
String importingLabel = itemRecord.getProperty().getLabel();
String existLabel = lastVersion.getProperty().getLabel();
for (IRepositoryViewObject currentVersion : allVersionToDelete) {
repFactory.forceDeleteObjectPhysical(lastVersion, currentVersion.getVersion(), isNeedDeleteOnRemote(importingLabel, existLabel));
}
idDeletedBeforeImport.add(id);
}
}
lastVersion = null;
// List<IRepositoryObject> list = cache.findObjectsByItem(itemRecord);
// if (!list.isEmpty()) {
// // this code will delete all version of item with same
// // id
// repFactory.forceDeleteObjectPhysical(list.get(0));
// deletedItems.add(id);
// }
}
User author = itemRecord.getProperty().getAuthor();
if (author != null) {
if (!repFactory.setAuthorByLogin(tmpItem, author.getLogin())) {
// author will be
tmpItem.getProperty().setAuthor(null);
// the logged
// user in
// create method
}
}
if (item instanceof JobletProcessItem) {
hasJoblets = true;
}
if (tmpItem instanceof ProcessItem && !statAndLogsSettingsReloaded && !implicitSettingsReloaded) {
ProcessItem processItem = (ProcessItem) tmpItem;
ParametersType paType = processItem.getProcess().getParameters();
boolean statsPSettingRemoved = false;
// for commanline import project setting
if (itemRecord.isRemoveProjectStatslog()) {
if (paType != null) {
String paramName = "STATANDLOG_USE_PROJECT_SETTINGS";
EList listParamType = paType.getElementParameter();
for (int j = 0; j < listParamType.size(); j++) {
ElementParameterType pType = (ElementParameterType) listParamType.get(j);
if (pType != null && paramName.equals(pType.getName())) {
pType.setValue(Boolean.FALSE.toString());
statsPSettingRemoved = true;
break;
}
}
}
}
// 14446: item apply project setting param if use project setting
String statslogUsePSetting = null;
String implicitUsePSetting = null;
if (paType != null) {
EList listParamType = paType.getElementParameter();
for (int j = 0; j < listParamType.size(); j++) {
ElementParameterType pType = (ElementParameterType) listParamType.get(j);
if (pType != null) {
if (!statsPSettingRemoved && "STATANDLOG_USE_PROJECT_SETTINGS".equals(pType.getName())) {
statslogUsePSetting = pType.getValue();
}
if ("IMPLICITCONTEXT_USE_PROJECT_SETTINGS".equals(pType.getName())) {
implicitUsePSetting = pType.getValue();
}
if (statsPSettingRemoved && implicitUsePSetting != null || !statsPSettingRemoved && implicitUsePSetting != null && statslogUsePSetting != null) {
break;
}
}
}
}
if (statslogUsePSetting != null && Boolean.parseBoolean(statslogUsePSetting) && !statAndLogsSettingsReloaded) {
CorePlugin.getDefault().getDesignerCoreService().reloadParamFromProjectSettings(paType, "STATANDLOG_USE_PROJECT_SETTINGS");
statAndLogsSettingsReloaded = true;
}
if (implicitUsePSetting != null && Boolean.parseBoolean(implicitUsePSetting) && !implicitSettingsReloaded) {
CorePlugin.getDefault().getDesignerCoreService().reloadParamFromProjectSettings(paType, "IMPLICITCONTEXT_USE_PROJECT_SETTINGS");
implicitSettingsReloaded = true;
}
}
if (lastVersion == null || itemRecord.getState().equals(ItemRecord.State.ID_EXISTED)) {
// import has not been developed to cope with migration in mind
// so some model may not be able to load like the ConnectionItems
// in that case items needs to be copied before migration
// here we check that the loading of the item failed before calling the create method
boolean isConnectionEmptyBeforeMigration = tmpItem instanceof ConnectionItem && ((ConnectionItem) tmpItem).getConnection().eResource() == null && !itemRecord.getMigrationTasksToApply().isEmpty();
repFactory.create(tmpItem, path, true);
if (isConnectionEmptyBeforeMigration) {
// copy the file before migration, this is bad because it
// should not refer to Filesytem
// but this is a quick hack and anyway the migration task only works on files
// IPath itemPath = itemRecord.getPath().removeFileExtension().addFileExtension(
// FileConstants.ITEM_EXTENSION);
InputStream is = manager.getStream(itemRecord.getPath().removeFileExtension().addFileExtension(FileConstants.ITEM_EXTENSION));
try {
URI propertyResourceURI = EcoreUtil.getURI(((ConnectionItem) tmpItem).getProperty());
URI relativePlateformDestUri = propertyResourceURI.trimFileExtension().appendFileExtension(FileConstants.ITEM_EXTENSION);
URL fileURL = FileLocator.toFileURL(new java.net.URL(//$NON-NLS-1$
"platform:/resource" + relativePlateformDestUri.toPlatformString(true)));
OutputStream os = new FileOutputStream(fileURL.getFile());
try {
FileCopyUtils.copyStreams(is, os);
} finally {
os.close();
}
} finally {
is.close();
}
repFactory.unloadResources(tmpItem.getProperty());
} else {
// connections from migrations (from 4.0.x or previous version) doesn't support reference or
// screenshots
// so no need to call this code.
// It's needed to avoid to call the save method mainly just before or after the copy of the old
// connection since it will
copyScreenshotFile(manager, itemRecord);
boolean haveRef = copyReferenceFiles(manager, tmpItem, itemRecord.getPath());
if (haveRef) {
repFactory.save(tmpItem, true);
}
}
itemRecord.setImportPath(path.toPortableString());
itemRecord.setRepositoryType(itemType);
itemRecord.setItemId(itemRecord.getProperty().getId());
itemRecord.setItemVersion(itemRecord.getProperty().getVersion());
itemRecord.setImported(true);
cache.addToCache(tmpItem);
} else if (VersionUtils.compareTo(lastVersion.getProperty().getVersion(), tmpItem.getProperty().getVersion()) < 0) {
repFactory.forceCreate(tmpItem, path);
itemRecord.setImportPath(path.toPortableString());
itemRecord.setItemId(itemRecord.getProperty().getId());
itemRecord.setRepositoryType(itemType);
itemRecord.setItemVersion(itemRecord.getProperty().getVersion());
itemRecord.setImported(true);
cache.addToCache(tmpItem);
} else {
PersistenceException e = new PersistenceException(Messages.getString("ImportItemUtil.persistenceException", //$NON-NLS-1$
tmpItem.getProperty()));
itemRecord.addError(e.getMessage());
logError(e);
}
if (tmpItem != null) {
// RelationshipItemBuilder.getInstance().addOrUpdateItem(tmpItem, true);
if (tmpItem.getState() != null) {
if (itemType != null) {
final Set<String> folders = restoreFolder.getFolders(itemType);
if (folders != null) {
for (String folderPath : folders) {
if (folderPath != null && folderPath.equals(path.toString())) {
FolderItem folderItem = repFactory.getFolderItem(ProjectManager.getInstance().getCurrentProject(), itemType, path);
if (folderItem != null) {
folderItem.getState().setDeleted(false);
while (!(folderItem.getParent() instanceof Project)) {
folderItem = (FolderItem) folderItem.getParent();
if (folderItem.getType() == FolderType.SYSTEM_FOLDER_LITERAL) {
break;
}
folderItem.getState().setDeleted(false);
}
}
break;
}
}
}
}
}
}
} catch (Exception e) {
itemRecord.addError(e.getMessage());
logError(e);
}
}
String label = itemRecord.getLabel();
EList<Resource> resources = itemRecord.getResourceSet().getResources();
Iterator<Resource> iterator = resources.iterator();
while (iterator.hasNext()) {
Resource res = iterator.next();
// it can't be unloaded just after create the item.
if (res != null && !(res instanceof ByteArrayResource)) {
res.unload();
iterator.remove();
}
}
TimeMeasure.step("importItemRecords", "Import item: " + label);
applyMigrationTasks(itemRecord, monitor);
TimeMeasure.step("importItemRecords", "applyMigrationTasks: " + label);
}
use of org.talend.core.model.properties.helper.ByteArrayResource in project tdq-studio-se by Talend.
the class TDQJrxmlItemImplTest method setUp.
/**
* DOC yyin Comment method "setUp".
*
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
jrxmlItem = PropertiesFactory.eINSTANCE.createTDQJrxmlItem();
EMFUtil emfUtil = new EMFUtil();
// File position = new File(""); //$NON-NLS-1$
// $NON-NLS-1$
file = new File("test_0.1.jrxml");
// $NON-NLS-1$
writeToFile("original jrxml content");
ResourceSet rs = emfUtil.getResourceSet();
// init the bytearray resource
ByteArray byteArray = org.talend.core.model.properties.PropertiesFactory.eINSTANCE.createByteArray();
byteArray.setInnerContentFromFile(file);
byteArrayResource = new ByteArrayResource(URI.createFileURI(file.getAbsolutePath()));
byteArrayResource.getContents().add(byteArray);
rs.getResources().add(byteArrayResource);
org.talend.core.model.properties.Property fileProperty = org.talend.core.model.properties.PropertiesFactory.eINSTANCE.createProperty();
fileProperty.setId(EcoreUtil.generateUUID());
fileProperty.setItem(jrxmlItem);
// $NON-NLS-1$
fileProperty.setLabel("test1");
// $NON-NLS-1$
jrxmlItem.setName("test1");
jrxmlItem.setProperty(fileProperty);
ItemState itemState = org.talend.core.model.properties.PropertiesFactory.eINSTANCE.createItemState();
// $NON-NLS-1$
itemState.setPath("data");
jrxmlItem.setState(itemState);
// get the eresource of the item without creating the real project
// $NON-NLS-1$
File property = new File("test_0.1.property");
Resource propertyResource = emfUtil.getResourceSet().createResource(URI.createFileURI(property.getAbsolutePath()));
propertyResource.getContents().add(jrxmlItem.getProperty());
propertyResource.getContents().add(jrxmlItem.getState());
propertyResource.getContents().add(jrxmlItem);
rs.getResources().add(propertyResource);
byteArrayResource.load(null);
jrxmlItem.setContent(byteArray);
emfUtil.save();
}
use of org.talend.core.model.properties.helper.ByteArrayResource in project tdq-studio-se by Talend.
the class TDQFileItemImpl method eResolveProxy.
// similar with FileItemImpl's same method, because this class can not extend that class.so copy the method from
// FileItemImpl.
@Override
public EObject eResolveProxy(InternalEObject proxy) {
URI proxyUri = proxy.eProxyURI();
URI resourceUri = proxyUri.trimFragment();
// bug 0020095
if (eResource() == null) {
return super.eResolveProxy(proxy);
}
ResourceSet resourceSet = eResource().getResourceSet();
ByteArrayResource byteArrayResource = null;
URIConverter theURIConverter = resourceSet.getURIConverter();
URI normalizedURI = theURIConverter.normalize(resourceUri);
// TUP-1814:because the routine proxyUri is File type,need handle with it.
if ((proxyUri.isPlatform() && proxyUri.segmentCount() > 1 && "resource".equals(proxyUri.segment(0))) || proxyUri.isFile()) {
List<Resource> resources = resourceSet.getResources();
synchronized (resources) {
for (Resource resource : resources) {
if (theURIConverter.normalize(resource.getURI()).equals(normalizedURI) && resource instanceof ByteArrayResource) {
byteArrayResource = (ByteArrayResource) resource;
break;
}
}
}
if (byteArrayResource == null) {
byteArrayResource = new ByteArrayResource(resourceUri);
resourceSet.getResources().add(byteArrayResource);
}
try {
byteArrayResource.load(null);
} catch (IOException e) {
}
} else {
List<Resource> resources = resourceSet.getResources();
synchronized (resources) {
for (Resource resource : resources) {
if (theURIConverter.normalize(resource.getURI()).equals(normalizedURI)) {
byteArrayResource = (ByteArrayResource) resource;
break;
}
}
}
}
if (byteArrayResource != null && byteArrayResource.isLoaded()) {
EObject object = byteArrayResource.getEObject(proxyUri.fragment().toString());
if (object != null) {
return object;
}
}
throw new UnsupportedOperationException();
}
use of org.talend.core.model.properties.helper.ByteArrayResource in project tdi-studio-se by Talend.
the class ImportItemUtil method createResource.
private Resource createResource(ItemRecord itemRecord, IPath path, boolean byteArrayResource) throws FileNotFoundException {
Resource resource;
ResourceSet resourceSet = itemRecord.getResourceSet();
if (byteArrayResource) {
resource = new ByteArrayResource(getURI(path));
resourceSet.getResources().add(resource);
} else {
if (FileConstants.ITEM_EXTENSION.equals(path.getFileExtension())) {
String projectName = "";
if (itemRecord.getItemProject() != null) {
projectName = itemRecord.getItemProject().getTechnicalLabel();
projectName = projectName.toLowerCase();
}
// note: do similar code as the CwmResourceFactory
//$NON-NLS-1$
String business = projectName + "/businessProcess/";
//$NON-NLS-1$
String context = projectName + "/context/";
//$NON-NLS-1$
String process = projectName + "/process/";
//$NON-NLS-1$
String joblet = projectName + "/joblets/";
String pathString = path.toPortableString();
pathString = pathString.toLowerCase();
// PTODO, maybe will bring bugs, like mr job,route, maybe jobscript
if (pathString.contains(process) || pathString.contains(context) || pathString.contains(business) || pathString.contains(joblet)) {
resource = new TalendXMIResource(getURI(path));
} else {
resource = new CwmResource(getURI(path));
}
resourceSet.getResources().add(resource);
} else {
resource = resourceSet.createResource(getURI(path));
}
}
return resource;
}
use of org.talend.core.model.properties.helper.ByteArrayResource in project tdq-studio-se by Talend.
the class SqlSourceFileUpdateTask method doExecute.
/*
* (non-Javadoc)
*
* @see org.talend.dataprofiler.core.migration.AMigrationTask#doExecute()
*/
@Override
protected boolean doExecute() throws Exception {
File rawFile = getWorkspacePath().append(TDQ_SOURCE_FILE_PATH).toFile();
EMFUtil emfUtil = new EMFUtil();
if (rawFile.exists() && rawFile.isDirectory()) {
ArrayList<File> fileList = new ArrayList<File>();
getAllFilesFromFolder(rawFile, fileList, new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.endsWith(SQL_EXT)) {
return true;
}
return false;
}
});
// $NON-NLS-1$
log.info("-----------Updating sql source file task ---------------");
int counter = 0;
for (File sqlFile : fileList) {
// $NON-NLS-1$
log.info(counter + ". Updating: " + sqlFile.getAbsolutePath());
// $NON-NLS-1$
String fileNamePart = FileUtils.getName(sqlFile) + "_" + VersionUtils.DEFAULT_VERSION;
IPath newSQLPath = new Path(sqlFile.getAbsolutePath()).removeLastSegments(1).append(fileNamePart).addFileExtension(SQL_EXT);
Property property = createProperty(sqlFile);
TDQFileItem item = (TDQFileItem) property.getItem();
URI itemResourceURI = URI.createFileURI(newSQLPath.toString());
Resource itemResource = new ByteArrayResource(itemResourceURI);
itemResource.getContents().add(item.getContent());
URI propertyResourceURI = itemResourceURI.trimFileExtension().appendFileExtension(PROP_EXT);
Resource propertyResource = emfUtil.getResourceSet().createResource(propertyResourceURI);
propertyResource.getContents().add(item.getProperty());
propertyResource.getContents().add(item.getState());
propertyResource.getContents().add(item);
EMFUtil.saveResource(itemResource);
EMFUtil.saveResource(propertyResource);
sqlFile.delete();
counter++;
}
emfUtil = null;
}
return true;
}
Aggregations