Skip to content

Commit

Permalink
Download only attachments by specified task types
Browse files Browse the repository at this point in the history
in addition fixed a bug where attachments would not download anymore due to superfluous API key header
  • Loading branch information
sepp87 committed Jun 6, 2024
1 parent 0d51111 commit 7d9ba07
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# How to
* Double click the Daluxify.jar to generate the app folder structure *
* Double click the Daluxify.jar to generate the app folder structure
* Navigate to the config folder and add your Dalux API key to api-key.txt
* Add the Dalux base URL to the base-url.txt file
* Add the project names of the projects you want to use to project-name.txt (each line represents a project)
* Add the task types you are interested in to task-types.txt (each line represents a task type)
* Now double click Daluxify.jar again and let the magic happen *


\*) alternatively you can execute the .jar through the command line to track the progess

5 changes: 5 additions & 0 deletions src/main/java/io/ost/dlx/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class App {
public static void main(String[] args) throws IOException {

TaskAttachmentsDownloader.download();

// List<Project> projects = getSelectedProjects();
// for (Project p: projects) {
// List<Task> tasks = Task.getTasks(p);
// }
}


Expand Down
9 changes: 8 additions & 1 deletion src/main/java/io/ost/dlx/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class Config {
private String baseUrl;
private String apiKey;
private List<String> projectNames;
private List<String> taskTypes;

private static final String CONFIG_DIRECTORY = "config" + File.separatorChar;
private static final String BUILD_DIRECTORY = "build" + File.separatorChar;
private static final String DONE_DIRECTORY = "done" + File.separatorChar;
private static final String API_KEY_FILE = CONFIG_DIRECTORY + "api-key.txt";
private static final String BASE_URL_FILE = CONFIG_DIRECTORY + "base-url.txt";
private static final String PROJECT_NAMES_FILE = CONFIG_DIRECTORY + "project-names.txt";
private static final String TASK_TYPES_FILE = CONFIG_DIRECTORY + "task-types.txt";

private Config() {
}
Expand All @@ -46,6 +48,7 @@ private static void loadConfig() {
config.baseUrl = Util.readFileAsString(new File(config.appRootDirectory + BASE_URL_FILE));
config.apiKey = Util.readFileAsString(new File(config.appRootDirectory + API_KEY_FILE));
config.projectNames = Util.readFileAsStringList(new File(config.appRootDirectory + PROJECT_NAMES_FILE));
config.taskTypes = Util.readFileAsStringList(new File(config.appRootDirectory + TASK_TYPES_FILE));

} catch (IOException ex) {
Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
Expand All @@ -59,7 +62,7 @@ private static void createFilesAndDirectories() {
Util.createFile(new File(config.appRootDirectory + BASE_URL_FILE));
Util.createFile(new File(config.appRootDirectory + API_KEY_FILE));
Util.createFile(new File(config.appRootDirectory + PROJECT_NAMES_FILE));

Util.createFile(new File(config.appRootDirectory + TASK_TYPES_FILE));
}

public String getAppRootDirectory() {
Expand All @@ -82,4 +85,8 @@ public List<String> getProjectNames() {
return projectNames;
}

public List<String> getTaskTypes() {
return taskTypes;
}

}
14 changes: 6 additions & 8 deletions src/main/java/io/ost/dlx/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -41,30 +42,27 @@ public static String get(String endpoint) {

public static void download(String endpoint, File target) {

String apiKey = Config.get().getApiKey();
String url = endpoint;

Request request = new Request.Builder()
.header("X-API-KEY", apiKey)
.url(url)
.url(endpoint)
.build();

OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);

try {
Response response = call.execute();
if(!response.isSuccessful()) {
if (!response.isSuccessful()) {
System.out.println("Download failed: " + response.code() + " " + response.message());
response.body().close();
return;
}
FileOutputStream fos = new FileOutputStream(target);
fos.write(response.body().bytes());
ResponseBody body = response.body();
fos.write(body.bytes());
fos.close();
} catch (IOException ex) {
Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, null, ex);
}

}

}
35 changes: 33 additions & 2 deletions src/main/java/io/ost/dlx/solutions/TaskAttachmentsDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.ost.dlx.api.Task;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -22,13 +23,17 @@ public class TaskAttachmentsDownloader {

public static void download() {
List<Project> projects = getSelectedProjects();

for (Project project : projects) {
Map<String, Task> tasks = getTasksAsMap(project);
List<Attachment> attachments = Attachment.getAttachments(project);
attachments = filterAttachmentsByAvailableTaskIds(attachments, tasks.keySet());
Map<String, String> filenames = getUniqueFilenames(attachments);

String projectDirectory = Config.get().getDoneDirectory() + project.projectName + File.separatorChar;
Util.createDirectory(new File(projectDirectory));
if (!attachments.isEmpty()) {
Util.createDirectory(new File(projectDirectory));
}

System.out.println();
System.out.println(attachments.size() + " Attachments to download for project " + project.projectName);
Expand Down Expand Up @@ -91,10 +96,36 @@ public static Map<String, Task> getTasksAsMap(Project project) {
Map<String, Task> result = new TreeMap<>();

List<Task> tasks = Task.getTasks(project);
tasks = filterTasksByType(tasks);
for (Task task : tasks) {
result.put(task.taskId, task);
}
return result;
}


private static List<Task> filterTasksByType(List<Task> tasks) {
List<String> types = Config.get().getTaskTypes();
if (types.isEmpty()) {
return tasks;
}

List<Task> result = new ArrayList<>();
for (Task task : tasks) {
if (types.contains(task.type.name)) {
result.add(task);
}
}
return result;
}

private static List<Attachment> filterAttachmentsByAvailableTaskIds(Collection<Attachment> attachments, Collection<String> taskIds) {
List<Attachment> result = new ArrayList<>();
for (Attachment attachment : attachments) {
if (taskIds.contains(attachment.taskId)) {
result.add(attachment);
}
}
return result;
}

}

0 comments on commit 7d9ba07

Please sign in to comment.