How to download a file from TestResource using JAVA OTA (COM4J) - java

Actually I need to download the XLS file from Test Resource using Resource ID in java
Can any one help me out Please
I tried with below pece of code but m missing something on it
IQCResourceFolderFactory rft = tdc.queryInterface(IQCResourceFolderFactory.class)​;
Com4jObject dfe = rft.item(3252);
IQCResourceFactory fds = dfe.queryInterface(IQCResourceFactory.class);
IList C = fds.newList("");
System.out.println(C.count());
above code throw me "Null pointer exception in Com4jObject dfe = rft.item(3252);
Please Help
Thanks in advance

Successfully downloaded the desired files from Test Resources by providing Resource Folder ID
Here is the working source code :
ITDConnection6 QCConnection = ClassFactory.createTDConnection();
QCConnection object should be declared with ITDConnection6 to access all QC attributes
IQCResourceFolderFactory resourceFolderFactory = QCConnection.qcResourceFolderFactory().queryInterface(IQCResourceFolderFactory.class);
IList folders = resourceFolderFactory.newList("");
for(Com4jObject rec : folders)
{
IQCResourceFolder resourceFolder = rec.queryInterface(IQCResourceFolder.class);
if(resourceFolder.id().toString().equals(properties.getProperty("ResourceFolderID")))
{
Com4jObject objResourceFactory = resourceFolder.qcResourceFactory();
IQCResourceFactory resourceFactory = objResourceFactory.queryInterface(IQCResourceFactory.class);
IList resources = resourceFactory.newList("");
for(Com4jObject objResource : resources)
{
IQCResource resource = objResource.queryInterface(IQCResource.class); ;
IResourceStorage resourceStorage = resource.queryInterface(IResourceStorage.class);
resourceStorage.downloadResource("D:\\", true);
}
}
}

Related

Sharepoint search file and copy from one location to another

String searchFile = "fileName.txt";
GraphServiceClient graphClient = (GraphServiceClient) GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
User me = graphClient.me().buildRequest().get();
ISiteRequestBuilder siteReq = graphClient.sites("sharepointSiteId");
IDriveRequestBuilder driveReq = siteReq.drive();
IDriveItemRequestBuilder driveRootReq = driveReq.root();
IDriveItemRequestBuilder source = driveRootReq.itemWithPath("sourcePath");
IDriveItemRequestBuilder destination = driveRootReq.itemWithPath("destinationPath");
IDriveItemCollectionRequestBuilder childrenReq = source.children();
String sourceId = source.buildRequest().get().id;
String destinationId = destination.buildRequest().get().id;
IDriveItemSearchCollectionPage searchResult = source.search(searchFile).buildRequest().get();
DriveItem fileResult = null;
for(DriveItem driveItem : searchResult.getCurrentPage()){
fileResult = driveItem;
}
if(fileResult!=null){
ItemReference parentReference = new ItemReference();
parentReference.id = destinationId;
driveReq.items(fileResult.id).copy(searchFile, parentReference).buildRequest().post();
}
I have this Java code to search a file on a sharepoint site location and copy it to another one.
It works ok but it has a bug, I cannot seem to be able to limit the search only to the source path and instead the search the whole site for the files with that name, so if I got multiple files with that name in the site it will bring them all in the results and that can mess up my copy.
Any one can help me solve this?
PS. if you have any pointers also to optimise this code they are also welcome.

How to get list of child files/directories having parent DataLakeDirectoryClient class instance

I have to scan whole data lake file system. Having code like:
PagedIterable<PathItem> pItems = ((DataLakeFileSystemClient)prmParent).listPaths();
for( PathItem pItem : pItems ){
if pItem.isDirectory() ){
((DataLakeFileSystemClient)prmParent).getDirectoryClient(pItem.getName());
} else {
((DataLakeFileSystemClient)prmParent).getFileClient(pItem.getName());
}
}
I get top level dirs/files. But to drill down there must be method listChild() in DataLakeDirectoryClient class.
But i did not find anything similar.
Does anybody know what is the proper way to walk thru the tree?
Thanks. Sergiy
If you want to list all path in Azure data lake gen2 file system, please refer to the following code
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
String endpoint = String.format(Locale.ROOT, "https://%s.dfs.core.windows.net", accountName);
DataLakeServiceClient storageClient = new DataLakeServiceClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
DataLakeFileSystemClient dataLakeFileSystemClient = storageClient.getFileSystemClient("test");
ListPathsOptions options = new ListPathsOptions();
options.setRecursive(true);
PagedIterable<PathItem> pItems = dataLakeFileSystemClient.listPaths(options,null);
for( PathItem pItem : pItems ){
if(pItem.isDirectory()) {
System.out.println("The directory: " +pItem.getName());
}else{
System.out.println("The file : " +pItem.getName());
}
}
For more details, please refer to here

how to fix file .json not found when it is there

I am trying here to consume the data from a json file:
DATA.json
[
{"Label"
:"USA",
"Adress":"This is the us",
"Lat":"36.9628066",
"Lng":"-122.0194722"
},
{ "Label" :"USA",
"Address":"2020",
"Lat":"36.9628066",
"Lng":"-122.0194722" }
]
Then applying it in my Mainclass:
using System.Collections.Generic;
using Xamarin.Forms.Maps;
using Xamarin.Forms;
using System.IO;
using Newtonsoft.Json;
using System;
namespace Orbage
{
class MapPage : ContentPage
{
public MapPage()
{
CustomMap customMap = new CustomMap
{
MapType = MapType.Street
};
// ...
Content = customMap;
var json = File.ReadAllText("File.json");
var places = JsonConvert.DeserializeObject<List<File>>(json);
foreach (var place in places)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = new Position(Double.Parse(place.Lat), Double.Parse(place.Lng)),
Label = place.Label,
Address = place.Address,
Name = "Xamarin",
Url = "http://xamarin.com/about/"
};
customMap.CustomPins = new List<CustomPin> { pin };
customMap.Pins.Add(pin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
}
}
}
}
But when I put the file in there it says that it doesn't exist.
Error:
FilenotfoundException
Whats the fix for this.
I tried:
Changing the location of the file.
Its name
Instead of E:/-/- i even wrote file.json but I still get the same error.
Thanks alot!
Try to use the method below:
Put your File.json to your PCL project,and set its Build Action to EmbeddedResource.
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MapPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream("Orbage.File.json");
string text = "";
using (var reader = new System.IO.StreamReader(stream))
{
text = reader.ReadToEnd();
}
Its better before reading the file, put check whether file exists in the location or not.
if (File.Exists("File.Json"))
With this you would become to know that your given path is correct or not.

How to download file in telegram bot (JAVA)

I want to download a file in my telegram bot code many tutorials say that I must use the getFile method that I can't find that in 4.2 version of the telegram API
so how I can download a file to a specific destination in host pc?
thanks
Assuming you are using TelegramBot SDK from rubenlagus (https://github.com/rubenlagus/TelegramBots), as I faced same issue. Below is my solution.
GetFile getFile = new GetFile().setFileId(fileId);
String filePath = execute(getFile).getFilePath();
File file = downloadFile(filePath, outputFile);
I had the same problem.
This was my solution. Not very nice but it works.
if (update.getMessage().hasDocument()){
String doc_id = update.getMessage().getDocument().getFileId();
String doc_name = update.getMessage().getDocument().getFileName();
String doc_mine = update.getMessage().getDocument().getMimeType();
int doc_size = update.getMessage().getDocument().getFileSize();
String getID = String.valueOf(update.getMessage().getFrom().getId());
Document document = new Document();
document.setMimeType(doc_mine);
document.setFileName(doc_name);
document.setFileSize(doc_size);
document.setFileId(doc_id);
GetFile getFile = new GetFile();
getFile.setFileId(document.getFileId());
try {
org.telegram.telegrambots.meta.api.objects.File file = execute(getFile);
downloadFile(file, new File("./data/userDoc/"+getID+"_"+doc_name));
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
Here I got this solution
enter link description here

fetching files from SFTP

I want to fetch files from SFTP which are created after a given timestamp(time of last pull) in java. I am using j2ssh as of now. Please let me know if some other API supports such a feature.
Jsch supports the ls command which will bring you back all the attributes of the remote file. You can write a little code to eliminate the files you want to retrieve from there.
Java Doc: http://epaul.github.io/jsch-documentation/javadoc/
This example compares the remote file timestamps to find the oldest file, it wouldn't be much of a stretch to modify it to compare your last run date against the remote file date, then do the download as part of the loop.
Code from Finding file size and last modified of SFTP file using Java
try {
list = Main.chanSftp.ls("*.xml");
if (list.isEmpty()) {
fileFound = false;
}
else {
lsEntry = (ChannelSftp.LsEntry) list.firstElement();
oldestFile = lsEntry.getFilename();
attrs = lsEntry.getAttrs();
currentOldestTime = attrs.getMTime();
for (Object sftpFile : list) {
lsEntry = (ChannelSftp.LsEntry) sftpFile;
nextName = lsEntry.getFilename();
attrs = lsEntry.getAttrs();
nextTime = attrs.getMTime();
if (nextTime < currentOldestTime) {
oldestFile = nextName;
currentOldestTime = nextTime;
}
}

Categories

Resources