package eu.openaire.urls_controller.controllers; import com.google.common.collect.HashMultimap; import eu.openaire.urls_controller.models.Assignment; import eu.openaire.urls_controller.models.Task; import eu.openaire.urls_controller.payloads.responces.AssignmentResponse; import eu.openaire.urls_controller.util.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.*; @RestController @RequestMapping("/urls") public class UrlController { private static final Logger logger = LoggerFactory.getLogger(UrlController.class); public UrlController() { } @GetMapping("") public ResponseEntity getUrls(@RequestParam String workerId, @RequestParam int tasksLimit) { List tasks = new ArrayList<>(); // TODO - Create the Assignment from the id-urls stored in the database up to the tasks-limit. Assignment assignment = new Assignment(tasks, workerId, new Date()); // TODO - Write the Assignment details to the database and then send it to the worker. return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment)); } @GetMapping("test") public ResponseEntity getTestUrls(@RequestParam String workerId, @RequestParam int tasksLimit) { List tasks = new ArrayList<>(); new FileUtils(); // Find the input file. HashMultimap loadedIdUrlPairs; boolean isFirstRun = true; boolean tasksLimitReached = false; // Start loading urls. while ( true ) { loadedIdUrlPairs = FileUtils.getNextIdUrlPairBatchFromJson(); // Take urls from jsonFile. if ( FileUtils.isFinishedLoading(loadedIdUrlPairs.isEmpty(), isFirstRun) ) // Throws RuntimeException which is automatically passed on. break; else isFirstRun = false; Set> pairs = loadedIdUrlPairs.entries(); for ( Map.Entry pair : pairs ) { if ( tasks.size() >= tasksLimit ) { tasksLimitReached = true; break; } tasks.add(new Task(pair.getKey(), pair.getValue())); }// end pairs-for-loop if ( tasksLimitReached ) { logger.debug("Done loading tasksLimit (" + tasksLimit + ") urls from the inputFile."); break; } }// end loading-while-loop Assignment assignment = new Assignment(tasks, workerId, new Date()); return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment)); } }