<geneiousWorkflows>
  <XMLSerialisableRootElement name="Find Big Folders" geneiousVersion="7.1.4" uniqueId="9212c3be-d264-4962-a9a9-fde6c946c357" revisionNumber="24" description="&lt;html&gt;Lists the size of all folders in your local database&lt;/html&gt;">
    <workflowElement type="com.biomatters.plugins.workflows.WorkflowElementCustomCode">
      <code>//This custom code iterates recursively over all folders in the local database, does a browse query to list
//their contents and adds up the total size of each. At the end a dialog is displayed with all folder sizes.
//WARNING: This breaks the workflow model slightly because there is no input or output, it just cancels the whole workflow.

public static List&lt;AnnotatedPluginDocument&gt; performOperation(List&lt;AnnotatedPluginDocument&gt; documents, Options options,
													  ProgressListener progressListener) throws DocumentOperationException {
	final Map&lt;Long, String&gt; map = new TreeMap&lt;Long, String&gt;();
	getFolderSizes(map, PluginUtilities.getGeneiousService(PluginUtilities.LOCAL_DATABASE_SERVICE_UNIQUE_ID), progressListener);

	StringBuilder sb = new StringBuilder();
	for (Map.Entry&lt;Long, String&gt; entry : map.entrySet()) {
		sb.append(StringUtilities.bytesToString(entry.getKey())).append(": ").append(entry.getValue()).append("\n");
	}

	Dialogs.DialogOptions dialogOptions = new Dialogs.DialogOptions(Dialogs.OK_ONLY, "Results");
	dialogOptions.setModal(false);
	Dialogs.showDialog(dialogOptions, sb.toString());
	throw new DocumentOperationException.Canceled();
}

public static void getFolderSizes(Map&lt;Long, String&gt; map, GeneiousService service, ProgressListener progressListener) throws DocumentOperationException {
	List&lt;GeneiousService&gt; childServices = service.getChildServices();
	for (GeneiousService childService : childServices) {
		if (progressListener.isCanceled()) {
			throw new DocumentOperationException.Canceled();
		}
		DatabaseService databaseService = (DatabaseService)childService;
		List&lt;AnnotatedPluginDocument&gt; docs;
		try {
			docs = (databaseService).retrieve(Query.Factory.createBrowseQuery(), ProgressListener.EMPTY);
		} catch (DatabaseServiceException e) {
			throw new DocumentOperationException(e);
		}
		Long totalSize = 0L;
		for (AnnotatedPluginDocument doc : docs) {
			Object size = doc.getFieldValue("document_size");
			if (size != null) {
				totalSize += ((Number)size).longValue();
			}
		}
		if (totalSize &gt; 0) {
			while (map.containsKey(totalSize)) {
				totalSize ++; //HACK since I cant use an anonymous inner class to sort the map by values :(
			}
			map.put(totalSize, databaseService.getFullPath());
		}
		//sb.append(StringUtilities.bytesToString(totalSize)).append(": ").append(databaseService.getFullPath()).append("\n");
		getFolderSizes(map, childService, progressListener);
	}
}</code>
    </workflowElement>
    <icon>iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADiElEQVR42u2XWUwTURSGi7gbjUYN8cElQjSBID65UVo3XBpZbLEWQSRiFBGNrRpLbDuA1LBThKZiqTZasAjGtaJglcXEBUQhVShL3WikWKVKXDAxvzP1xeiDA1QTDSeZh8m95/zf3HPm5B4GY8gGYSaTyd1qtY6w2Wwj7Xb7KJPDMYZ6/2OCw/wJ35ErEmPHrk7aOX5timRsYGLh+DUpmmmhadLZgmzRnMicfZEp51ZV1Ft8rtS0eJXWtHgQJOSABb29CXc3lix6GFtWzmBJe6fzc8HeVwKmUI/l4vMIlVdiXXIlFu4thW9sEbx36DAxOAuMpQcdE9bKK+bHKJOkGmNoZnHV3H6LjwpMFA9fJgNXVgrFhSaU1XVDd9cOTW03lDe7kHX9FY4YrJBd6MTBshcQ6p8jTvcU27QWCFTNYMuq4Bmjw+jAwyBBtHsUhgW0hAmCcJsadCRvuVCLutZumLs+4W5HLwxNPbQBojQdCC9oxwZVG9ZkNsKDr0JIQvEbLlE26bcAk7nyKXMiFLC//wzbuy+DBgjNbwUn2wT/XYWYxcv2pFVoS+LUeNPbhy5H36ABgnMfw190FjMFuaBi0wLwjc7H63ef0fzSgYdPHag19+BK41vaAJHqVoRk3ANLfBl+W1UISLiKCUFZ/QP40PfVeQIPLD0w1Ftx0tiOvPI25BjaISlqwH7NfYi0DYg7Xo/I7Cpw5dfAIQxYGH8KfjEqLN6rx0riBoKPNiPoqHlgAD/XgOrGcySVNCLhdAPij91BRHoFeCnl4KcZIcisQVh6NSKUj36pAZcBU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