Search code examples
javaartifactory

How to get file list of an Artifactory folder sorted by last modified stamp from Java client


I am trying to get last modified time stamp on Artifactory file item, but it is coming as null. What am I doing wrong with this function?

    public static List<File> listChildren(RepositoryHandle aRepo, String folder, FileFilter filter)
    {
        List<File> children = null;
        
        if (null == aRepo || null == folder || folder.length() <= 0)
            return null;
        try
        {
            folder = FilenameUtils.normalizeNoEndSeparator(folder, true);
            ItemHandle folderItem = aRepo.folder(folder);
            Folder folderInfo = folderItem.info();
            List<Item> itemChildren = folderInfo.getChildren();
            if (null == itemChildren)
                return null;
            children = new ArrayList<>(itemChildren.size());
            String name;
            File fileItem;
            for(Item curItem: itemChildren)
            {
                
                fileItem = new File(curItem.getUri());
                Date timeStamp = curItem.getLastModified(); // This is coming as null
                fileItem.setLastModified(curItem.getLastModified().getTime());
                name = fileItem.getName();
                if (null != name)
                {
                    if (null == filter ||
                        filter.accept(fileItem))
                        children.add(fileItem);
                }
            } 
            // Sort the list by last modified stamp so that the oldest is at the front
            Collections.sort(children, Comparator.comparingLong(File::lastModified));
        }
        catch (Throwable ex)
        {
            ex.printStackTrace();
        }
        return children;
    }

Solution

  • Below is modified version that gave me sorted list.

    public static List<String> listChildren(RepositoryHandle aRepo, String folder, FileFilter filter)
        {
            List<String> children = null;
            
            if (null == aRepo || null == folder || folder.length() <= 0)
                return null;
            try
            {
                folder = FilenameUtils.normalizeNoEndSeparator(folder, true);
                ItemHandle folderItem = aRepo.folder(folder);
                Folder folderInfo = folderItem.info();
                List<Item> itemChildren = folderInfo.getChildren();
                if (null == itemChildren)
                    return null;
                List<org.jfrog.artifactory.client.model.File> itemExtChildren = new ArrayList<>();
                for(Item curItem: itemChildren)
                {
                    ItemHandle hItem = aRepo.file(folder+curItem.getUri());
                    if (null == hItem)
                        continue;
                    org.jfrog.artifactory.client.model.File aFile = hItem.info();
                    itemExtChildren.add(aFile);
                }
                Collections.sort(itemExtChildren, (org.jfrog.artifactory.client.model.File f1, org.jfrog.artifactory.client.model.File f2) -> 
                                                  f1.getLastModified().compareTo(f2.getLastModified()));
                children = new ArrayList<>(itemChildren.size());
                String name;
                for(Item curItem: itemExtChildren)
                {
                    name = curItem.getName();
                    if (null != name)
                    {
                        if (null == filter ||
                            filter.accept(new File(name)))
                            children.add(name);
                    }
                } 
            }
            catch (Throwable ex)
            {
            }
            return children;
        }