Compare commits

...

2 Commits

Author SHA1 Message Date
Gustavo Henrique Miranda f1b4033c45
Merge 80ee003fc9 into ae6f522901 2025-12-09 19:43:55 +00:00
Gustavo Henrique Santos Souza de Miranda 80ee003fc9 Implement trackhascomposer relationship management
Add complete implementation for track-composer relationship management,
  following the established trackhasgenre pattern. This enables tracking
  which composers are associated with each track in the media library.

  Changes:
  - Enhance TrackHasComposer model: Add @Entity and @Table annotations,
    nullable constraints, constructor, and toString method
  - Implement TrackHasComposerRepository with full CRUD operations
  - Implement TrackHasComposerService with business logic and validation
  - Add TrackHasComposerMapper for entity/protobuf conversion
  - Create action handlers:
    * CreateTrackHasComposerHandler (trackhascomposer.create)
    * DeleteTrackHasComposerHandler (trackhascomposer.delete)
    * GetTrackHasComposerByIdHandler (trackhascomposer.getById)
    * GetTrackHasComposerHandler (trackhascomposer.getAll)
  - Register TrackHasComposerService in DelegateActionManager for
    automatic handler discovery and dependency injection
  - Proto file (trackhascomposer.proto) was already correct

  The implementation validates track and composer existence before creating
  relationships and provides proper error handling with appropriate HTTP
  status codes.

  🤖 Generated with [Claude Code](https://claude.com/claude-code)

  Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-09 16:43:47 -03:00
10 changed files with 535 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package com.mediamanager.mapper;
import com.mediamanager.model.TrackHasComposer;
import com.mediamanager.protocol.messages.TrackHasComposerMessages;
public class TrackHasComposerMapper {
public static TrackHasComposerMessages.TrackHasComposer toProtobuf(TrackHasComposer entity) {
if (entity == null) {
return null;
}
TrackHasComposerMessages.TrackHasComposer.Builder builder = TrackHasComposerMessages.TrackHasComposer.newBuilder();
Integer id = entity.getId();
if (id != null) {
builder.setId(id);
}
// Map Track foreign key
if (entity.getTrack() != null && entity.getTrack().getId() != null) {
builder.setFkTrackId(entity.getTrack().getId());
}
// Map Composer foreign key
if (entity.getComposer() != null && entity.getComposer().getId() != null) {
builder.setFkComposerId(entity.getComposer().getId());
}
return builder.build();
}
public static TrackHasComposer toEntity(TrackHasComposerMessages.TrackHasComposer protobuf) {
if (protobuf == null) {
return null;
}
TrackHasComposer entity = new TrackHasComposer();
if (protobuf.getId() > 0) {
entity.setId(protobuf.getId());
}
// Note: Foreign key relationships (Track, Composer) are handled in the service layer
return entity;
}
}

View File

@ -0,0 +1,55 @@
package com.mediamanager.model;
import jakarta.persistence.*;
@Entity
@Table(name = "trackhascomposer")
public class TrackHasComposer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_track_id", nullable = false)
private Track track;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_composer_id", nullable = false)
private Composer composer;
public TrackHasComposer() {}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Track getTrack() {
return track;
}
public void setTrack(Track track) {
this.track = track;
}
public Composer getComposer() {
return composer;
}
public void setComposer(Composer composer) {
this.composer = composer;
}
@Override
public String toString() {
return "TrackHasComposer{" +
"id=" + id +
", trackId=" + (track != null ? track.getId() : null) +
", composerId=" + (composer != null ? composer.getId() : null) +
'}';
}
}

View File

@ -0,0 +1,85 @@
package com.mediamanager.repository;
import com.mediamanager.model.TrackHasComposer;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.Optional;
public class TrackHasComposerRepository {
private static final Logger logger = LogManager.getLogger(TrackHasComposerRepository.class);
private final EntityManagerFactory entityManagerFactory;
public TrackHasComposerRepository(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
public TrackHasComposer save(TrackHasComposer trackHasComposer) {
logger.debug("Saving TrackHasComposer: {}", trackHasComposer);
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
try {
em.persist(trackHasComposer);
em.getTransaction().commit();
logger.debug("TrackHasComposer has been saved successfully");
return trackHasComposer;
} catch (Exception e) {
em.getTransaction().rollback();
logger.error("Error while saving TrackHasComposer: {}", e.getMessage());
throw e;
} finally {
if (em.isOpen()) em.close();
}
}
public List<TrackHasComposer> findAll() {
logger.debug("Finding All TrackHasComposer");
EntityManager em = entityManagerFactory.createEntityManager();
try{
return em.createQuery("select t from TrackHasComposer t", TrackHasComposer.class).getResultList();
}finally {
if (em.isOpen()) em.close();
}
}
public Optional<TrackHasComposer> findById(Integer id) {
logger.debug("Finding TrackHasComposer with id: {}", id);
EntityManager em = entityManagerFactory.createEntityManager();
try{
TrackHasComposer trackHasComposer = em.find(TrackHasComposer.class, id);
return Optional.ofNullable(trackHasComposer);
}finally {
if (em.isOpen()) em.close();
}
}
public boolean deleteById(Integer id){
logger.debug("Deleting TrackHasComposer with id: {}", id);
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
try{
TrackHasComposer trackHasComposer = em.find(TrackHasComposer.class, id);
if (trackHasComposer == null) {
em.getTransaction().rollback();
return false;
}
em.remove(trackHasComposer);
em.getTransaction().commit();
logger.debug("TrackHasComposer has been deleted successfully");
return true;
} catch (Exception e) {
em.getTransaction().rollback();
logger.error("Error while deleting TrackHasComposer: {}", e.getMessage());
throw e;
} finally {
if (em.isOpen()) em.close();
}
}
}

View File

@ -14,6 +14,7 @@ import com.mediamanager.service.bitrate.BitRateService;
import com.mediamanager.service.composer.ComposerService;
import com.mediamanager.service.trackhasgenre.TrackHasGenreService;
import com.mediamanager.service.trackhasartist.TrackHasArtistService;
import com.mediamanager.service.trackhascomposer.TrackHasComposerService;
import com.mediamanager.repository.GenreRepository;
import com.mediamanager.service.artist.ArtistService;
@ -122,6 +123,10 @@ public class DelegateActionManager {
TrackHasArtistService trackHasArtistService = new TrackHasArtistService(trackHasArtistRepository, trackRepository, artistRepository);
serviceLocator.register(TrackHasArtistService.class, trackHasArtistService);
TrackHasComposerRepository trackHasComposerRepository = new TrackHasComposerRepository(entityManagerFactory);
TrackHasComposerService trackHasComposerService = new TrackHasComposerService(trackHasComposerRepository, trackRepository, composerRepository);
serviceLocator.register(TrackHasComposerService.class, trackHasComposerService);
serviceLocator.logRegisteredServices();
logger.info("Services initialized successfully");

View File

@ -0,0 +1,54 @@
package com.mediamanager.service.delegate.handler.trackhascomposer;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.mediamanager.mapper.TrackHasComposerMapper;
import com.mediamanager.model.TrackHasComposer;
import com.mediamanager.protocol.TransportProtocol;
import com.mediamanager.protocol.messages.TrackHasComposerMessages;
import com.mediamanager.service.delegate.ActionHandler;
import com.mediamanager.service.delegate.annotation.Action;
import com.mediamanager.service.trackhascomposer.TrackHasComposerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Action("trackhascomposer.create")
public class CreateTrackHasComposerHandler implements ActionHandler {
private static final Logger logger = LogManager.getLogger(CreateTrackHasComposerHandler.class);
private final TrackHasComposerService trackHasComposerService;
public CreateTrackHasComposerHandler(TrackHasComposerService trackHasComposerService) {
this.trackHasComposerService = trackHasComposerService;
}
@Override
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
try{
TrackHasComposerMessages.CreateTrackHasComposerRequest createRequest =
TrackHasComposerMessages.CreateTrackHasComposerRequest.parseFrom(requestPayload);
TrackHasComposer trackHasComposer = trackHasComposerService.createTrackHasComposer(
createRequest.getFkTrackId() > 0 ? createRequest.getFkTrackId() : null,
createRequest.getFkComposerId() > 0 ? createRequest.getFkComposerId() : null
);
TrackHasComposerMessages.TrackHasComposer trackHasComposerProto = TrackHasComposerMapper.toProtobuf(trackHasComposer);
TrackHasComposerMessages.CreateTrackHasComposerResponse createTrackHasComposerResponse = TrackHasComposerMessages.CreateTrackHasComposerResponse.newBuilder()
.setTrackhascomposer(trackHasComposerProto)
.build();
return TransportProtocol.Response.newBuilder()
.setPayload(createTrackHasComposerResponse.toByteString());
} catch (IllegalArgumentException e) {
logger.error("Validation error", e);
return TransportProtocol.Response.newBuilder()
.setStatusCode(400)
.setPayload(ByteString.copyFromUtf8("Validation error: " + e.getMessage()));
} catch (Exception e) {
logger.error("Error creating track has composer", e);
return TransportProtocol.Response.newBuilder()
.setStatusCode(500)
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
}
}
}

View File

@ -0,0 +1,62 @@
package com.mediamanager.service.delegate.handler.trackhascomposer;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.mediamanager.protocol.TransportProtocol;
import com.mediamanager.protocol.messages.TrackHasComposerMessages;
import com.mediamanager.service.delegate.ActionHandler;
import com.mediamanager.service.delegate.annotation.Action;
import com.mediamanager.service.trackhascomposer.TrackHasComposerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Action("trackhascomposer.delete")
public class DeleteTrackHasComposerHandler implements ActionHandler {
private static final Logger logger = LogManager.getLogger(DeleteTrackHasComposerHandler.class);
private final TrackHasComposerService trackHasComposerService;
public DeleteTrackHasComposerHandler(TrackHasComposerService trackHasComposerService) {
this.trackHasComposerService = trackHasComposerService;
}
@Override
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
throws InvalidProtocolBufferException {
try {
TrackHasComposerMessages.DeleteTrackHasComposerRequest deleteRequest =
TrackHasComposerMessages.DeleteTrackHasComposerRequest.parseFrom(requestPayload);
int id = deleteRequest.getId();
boolean success = trackHasComposerService.deleteTrackHasComposer(id);
TrackHasComposerMessages.DeleteTrackHasComposerResponse deleteResponse;
if (success) {
deleteResponse = TrackHasComposerMessages.DeleteTrackHasComposerResponse.newBuilder()
.setSuccess(true)
.setMessage("Track has composer deleted successfully")
.build();
return TransportProtocol.Response.newBuilder()
.setPayload(deleteResponse.toByteString());
} else {
deleteResponse = TrackHasComposerMessages.DeleteTrackHasComposerResponse.newBuilder()
.setSuccess(false)
.setMessage("Track has composer not found")
.build();
return TransportProtocol.Response.newBuilder()
.setStatusCode(404)
.setPayload(deleteResponse.toByteString());
}
} catch (Exception e) {
logger.error("Error deleting track has composer", e);
TrackHasComposerMessages.DeleteTrackHasComposerResponse deleteResponse =
TrackHasComposerMessages.DeleteTrackHasComposerResponse.newBuilder()
.setSuccess(false)
.setMessage("Error: " + e.getMessage())
.build();
return TransportProtocol.Response.newBuilder()
.setStatusCode(500)
.setPayload(deleteResponse.toByteString());
}
}
}

View File

@ -0,0 +1,56 @@
package com.mediamanager.service.delegate.handler.trackhascomposer;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.mediamanager.mapper.TrackHasComposerMapper;
import com.mediamanager.model.TrackHasComposer;
import com.mediamanager.protocol.TransportProtocol;
import com.mediamanager.protocol.messages.TrackHasComposerMessages;
import com.mediamanager.service.delegate.ActionHandler;
import com.mediamanager.service.delegate.annotation.Action;
import com.mediamanager.service.trackhascomposer.TrackHasComposerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Optional;
@Action(value = "trackhascomposer.getById")
public class GetTrackHasComposerByIdHandler implements ActionHandler {
private static final Logger logger = LogManager.getLogger(GetTrackHasComposerByIdHandler.class);
private final TrackHasComposerService trackHasComposerService;
public GetTrackHasComposerByIdHandler(TrackHasComposerService trackHasComposerService) {
this.trackHasComposerService = trackHasComposerService;
}
@Override
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
throws InvalidProtocolBufferException{
try{
TrackHasComposerMessages.GetTrackHasComposerByIdRequest getByIdRequest =
TrackHasComposerMessages.GetTrackHasComposerByIdRequest.parseFrom(requestPayload);
int id = getByIdRequest.getId();
Optional<TrackHasComposer> trackHasComposerOpt = trackHasComposerService.getTrackHasComposerById(id);
if (trackHasComposerOpt.isEmpty()){
logger.warn("TrackHasComposer not found with ID: {}", id);
return TransportProtocol.Response.newBuilder()
.setStatusCode(404)
.setPayload(ByteString.copyFromUtf8("TrackHasComposer not found"));
}
TrackHasComposerMessages.TrackHasComposer trackHasComposerProto = TrackHasComposerMapper.toProtobuf(trackHasComposerOpt.get());
TrackHasComposerMessages.GetTrackHasComposerByIdResponse getByIdResponse = TrackHasComposerMessages.GetTrackHasComposerByIdResponse.newBuilder()
.setTrackhascomposer(trackHasComposerProto)
.build();
return TransportProtocol.Response.newBuilder()
.setPayload(getByIdResponse.toByteString());
} catch (Exception e) {
logger.error("Error getting track has composer by ID", e);
return TransportProtocol.Response.newBuilder()
.setStatusCode(500)
.setPayload(ByteString.copyFromUtf8("Error: "+ e.getMessage()));
}
}
}

View File

@ -0,0 +1,48 @@
package com.mediamanager.service.delegate.handler.trackhascomposer;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.mediamanager.mapper.TrackHasComposerMapper;
import com.mediamanager.model.TrackHasComposer;
import com.mediamanager.protocol.TransportProtocol;
import com.mediamanager.protocol.messages.TrackHasComposerMessages;
import com.mediamanager.service.delegate.ActionHandler;
import com.mediamanager.service.delegate.annotation.Action;
import com.mediamanager.service.trackhascomposer.TrackHasComposerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
@Action("trackhascomposer.getAll")
public class GetTrackHasComposerHandler implements ActionHandler {
private static final Logger logger = LogManager.getLogger(GetTrackHasComposerHandler.class);
private final TrackHasComposerService trackHasComposerService;
public GetTrackHasComposerHandler(TrackHasComposerService trackHasComposerService){this.trackHasComposerService = trackHasComposerService;}
@Override
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
try{
List<TrackHasComposer> trackHasComposers = trackHasComposerService.getAllTrackHasComposers();
TrackHasComposerMessages.GetTrackHasComposersResponse.Builder responseBuilder = TrackHasComposerMessages.GetTrackHasComposersResponse.newBuilder();
for (TrackHasComposer trackHasComposer : trackHasComposers) {
TrackHasComposerMessages.TrackHasComposer trackHasComposerProto = TrackHasComposerMapper.toProtobuf(trackHasComposer);
responseBuilder.addTrackhascomposer(trackHasComposerProto);
}
TrackHasComposerMessages.GetTrackHasComposersResponse getTrackHasComposersResponse = responseBuilder.build();
return TransportProtocol.Response.newBuilder()
.setPayload(getTrackHasComposersResponse.toByteString());
}catch (Exception e){
logger.error("Error getting track has composers", e);
return TransportProtocol.Response.newBuilder()
.setStatusCode(500)
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
}
}
}

View File

@ -0,0 +1,77 @@
package com.mediamanager.service.trackhascomposer;
import com.mediamanager.model.Track;
import com.mediamanager.model.TrackHasComposer;
import com.mediamanager.model.Composer;
import com.mediamanager.repository.TrackHasComposerRepository;
import com.mediamanager.repository.TrackRepository;
import com.mediamanager.repository.ComposerRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.Optional;
public class TrackHasComposerService {
private static final Logger logger = LogManager.getLogger(TrackHasComposerService.class);
private final TrackHasComposerRepository repository;
private final TrackRepository trackRepository;
private final ComposerRepository composerRepository;
public TrackHasComposerService(TrackHasComposerRepository repository, TrackRepository trackRepository, ComposerRepository composerRepository) {
this.repository = repository;
this.trackRepository = trackRepository;
this.composerRepository = composerRepository;
}
public TrackHasComposer createTrackHasComposer(Integer trackId, Integer composerId) {
logger.debug("Creating track has composer relationship - trackId:{}, composerId:{}", trackId, composerId);
if (trackId == null || trackId <= 0) {
throw new IllegalArgumentException("Track ID cannot be null or invalid");
}
if (composerId == null || composerId <= 0) {
throw new IllegalArgumentException("Composer ID cannot be null or invalid");
}
// Verify Track exists
Optional<Track> track = trackRepository.findById(trackId);
if (track.isEmpty()) {
throw new IllegalArgumentException("Track not found with id: " + trackId);
}
// Verify Composer exists
Optional<Composer> composer = composerRepository.findById(composerId);
if (composer.isEmpty()) {
throw new IllegalArgumentException("Composer not found with id: " + composerId);
}
TrackHasComposer trackHasComposer = new TrackHasComposer();
trackHasComposer.setTrack(track.get());
trackHasComposer.setComposer(composer.get());
return repository.save(trackHasComposer);
}
public List<TrackHasComposer> getAllTrackHasComposers() {
logger.info("Getting all track has composer relationships");
return repository.findAll();
}
public Optional<TrackHasComposer> getTrackHasComposerById(Integer id) {
if (id == null) {
throw new IllegalArgumentException("ID cannot be null");
}
logger.info("Getting track has composer by id:{}", id);
return repository.findById(id);
}
public boolean deleteTrackHasComposer(Integer id) {
if (id == null) {
throw new IllegalArgumentException("Track has composer id cannot be null");
}
logger.info("Deleting track has composer:{}", id);
return repository.deleteById(id);
}
}

View File

@ -0,0 +1,46 @@
syntax = "proto3";
option java_package = "com.mediamanager.protocol.messages";
option java_outer_classname = "TrackHasComposerMessages";
package mediamanager.messages;
message TrackHasComposer{
int32 id = 1;
int32 fk_track_id = 2;
int32 fk_composer_id =3;
}
message CreateTrackHasComposerRequest {
int32 fk_track_id = 1;
int32 fk_composer_id = 2;
}
message CreateTrackHasComposerResponse {
TrackHasComposer trackhascomposer = 1;
}
message GetTrackHasComposersRequest {
}
message GetTrackHasComposersResponse {
repeated TrackHasComposer trackhascomposer = 1;
}
message GetTrackHasComposerByIdRequest {
int32 id = 1;
}
message GetTrackHasComposerByIdResponse {
TrackHasComposer trackhascomposer = 1;
}
message DeleteTrackHasComposerRequest {
int32 id = 1;
}
message DeleteTrackHasComposerResponse {
bool success = 1;
string message = 2;
}