-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement design creation incl. persistence
- Loading branch information
Showing
18 changed files
with
341 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
parent/addons/src/main/java/com/pim/stars/design/api/types/DesignType.java
This file was deleted.
Oops, something went wrong.
114 changes: 114 additions & 0 deletions
114
parent/addons/src/main/java/com/pim/stars/design/imp/DesignDefinerImp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.pim.stars.design.imp; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.pim.stars.design.api.Design; | ||
import com.pim.stars.design.api.DesignDefiner; | ||
import com.pim.stars.design.imp.persistence.DesignEntity; | ||
import com.pim.stars.design.imp.persistence.DesignEntity.DesignEntityId; | ||
import com.pim.stars.design.imp.persistence.DesignEntity.DesignEntityStatus; | ||
import com.pim.stars.design.imp.persistence.DesignEntity.FilledGadgetSlot; | ||
import com.pim.stars.design.imp.persistence.DesignRepository; | ||
import com.pim.stars.gadget.api.Gadget; | ||
import com.pim.stars.gadget.api.hull.GadgetSlot; | ||
import com.pim.stars.gadget.api.hull.Hull; | ||
import com.pim.stars.gadget.api.types.HullType; | ||
import com.pim.stars.game.api.Game; | ||
import com.pim.stars.id.api.IdCreator; | ||
import com.pim.stars.race.api.Race; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Component | ||
public class DesignDefinerImp implements DesignDefiner { | ||
|
||
@Autowired | ||
private IdCreator idCreator; | ||
@Autowired | ||
private DesignRepository designRepository; | ||
|
||
@Override | ||
public DesignBuilder start(final Game game, final Race race, final Hull hull) { | ||
return new DesignBuilderImp(game, race.getId(), hull); | ||
} | ||
|
||
private class DesignBuilderImp implements DesignBuilder { | ||
|
||
private final DesignEntity entity = new DesignEntity(); | ||
private final Hull hull; | ||
|
||
public DesignBuilderImp(final Game game, final String ownerId, final Hull hull) { | ||
entity.setEntityId(new DesignEntityId(idCreator.createId(), game.getId(), game.getYear())); | ||
entity.setOwnerId(ownerId); | ||
entity.setHullId(hull.getId()); | ||
this.hull = hull; | ||
} | ||
|
||
@Override | ||
public DesignBuilder fillSlot(final String slotId, final Gadget gadget, final int numberOfGadgets) { | ||
final GadgetSlot slot = hull.getGadgetSlots().stream() | ||
.filter(candidate -> candidate.getSlotId().equals(slotId)).findFirst() | ||
.orElseThrow(() -> incorrectSlotException(hull.getId(), slotId)); | ||
return fillSlot(slot, gadget, numberOfGadgets); | ||
} | ||
|
||
private DesignBuilder fillSlot(final GadgetSlot slot, final Gadget gadget, final int numberOfGadgets) { | ||
final String slotId = slot.getSlotId(); | ||
ensureSlotNotAlreadyFilled(slotId); | ||
ensureGadgetAllowedInSlot(slot, gadget, numberOfGadgets); | ||
|
||
entity.getFilledGadgetSlots().add(new FilledGadgetSlot(slotId, gadget.getId(), numberOfGadgets)); | ||
|
||
return this; | ||
} | ||
|
||
private void ensureSlotNotAlreadyFilled(final String slotId) { | ||
final boolean slotIsAlreadyFilled = entity.getFilledGadgetSlots().stream() | ||
.anyMatch(candidate -> candidate.getSlotId().equals(slotId)); | ||
if (slotIsAlreadyFilled) { | ||
throw new IllegalArgumentException("The slot with ID " + slotId + " has already been filled."); | ||
} | ||
} | ||
|
||
private void ensureGadgetAllowedInSlot(final GadgetSlot slot, final Gadget gadget, final int numberOfGadgets) { | ||
if (!slot.getAllowedGadgetTypes().stream() | ||
.anyMatch(type -> type.getId().equals(gadget.getGadgetType().getId()))) { | ||
throw new IllegalArgumentException("The slot with ID " + slot.getSlotId() | ||
+ " does not allow gadget with ID " + gadget.getId() + ", because type " | ||
+ gadget.getGadgetType() + " is not contained in the list of allowed gadget types: " | ||
+ slot.getAllowedGadgetTypes()); | ||
} else if ((slot.getMinimumNumberOfGadgets() > numberOfGadgets) | ||
|| (slot.getMaximumNumberOfGadgets() < numberOfGadgets)) { | ||
throw new IllegalArgumentException("The slot with ID " + slot.getSlotId() + " does not allow to add " | ||
+ numberOfGadgets + ". It must be between " + slot.getMinimumNumberOfGadgets() + " and " | ||
+ slot.getMaximumNumberOfGadgets() + "."); | ||
} | ||
} | ||
|
||
@Override | ||
public Design build(final String designName) { | ||
entity.setName(designName); | ||
entity.setHullTypeId(hull.getHullType().getId()); | ||
entity.setStatus(DesignEntityStatus.EDITABLE.getId()); | ||
designRepository.save(entity); | ||
return new DesignImp(entity.getEntityId().getId(), entity.getName(), entity.getOwnerId(), | ||
hull.getHullType()); | ||
} | ||
|
||
private IllegalArgumentException incorrectSlotException(final String hullId, final String slotId) { | ||
return new IllegalArgumentException("The hull with the ID " + hullId + " has no slot with ID " + slotId); | ||
} | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
private class DesignImp implements Design { | ||
|
||
private final String id; | ||
private final String name; | ||
private final String ownerId; | ||
private final HullType hullType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 11 additions & 1 deletion
12
parent/addons/src/test/java/com/pim/stars/design/DesignTestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.