Untitled
unknown
plain_text
8 months ago
2.0 kB
5
Indexable
package com.sber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.UUID;
@Component
public class MyShinyService {
@Autowired
private final TaskRepository taskRepository;
@Autowired
private final WorkRepository workRepository;
// for concurrency safety
private final Task task;
@Transactional
public synchronized void accumulateTask(Task task) {
this.task = task;
}
@Transactional
public synchronized void saveTask() {
System.out.println("saving process has started");
String task = "task";
if (this.task.type == task) {
this.taskRepository.save(new TaskEntity(
UUID.randomUUID(),
this.task.id.toString(),
this.task.name
));
} else {
this.workRepository.save(new WorkEntity(
UUID.randomUUID(),
this.task.id.toString(),
this.task.name,
null
));
}
System.out.println("saved");
}
public interface ExternalTask {
// interface methods to be implemented
}
public static class Task {
public int id;
public String name;
public String type;
// other fields and methods
}
}
// Existing code somewhere in the project
public class TaskRepository {
public UUID save(TaskEntity entity) {
// implementation
return null;
}
}
record TaskEntity(UUID id, String outerId, String name) {
}
public class WorkRepository {
public UUID save(TaskEntity entity) {
// implementation
return null;
}
}
record WorkEntity(UUID id, String outerId, String name, String author) {
}Editor is loading...
Leave a Comment