Skip to content

Commit

Permalink
Merge pull request #50 from OpenLiberty/findByShipSize
Browse files Browse the repository at this point in the history
Adding findByShipSizeAndRank
  • Loading branch information
mswatosh authored Dec 11, 2023
2 parents cf13333 + c18a6b6 commit 8e5d288
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<!-- Plugins -->
<version.liberty-maven-plugin>3.10</version.liberty-maven-plugin>
<!-- OpenLiberty runtime -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package io.openliberty.sample.application;


import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotEmpty;
Expand All @@ -30,8 +31,13 @@ public class CrewMember {
@Positive(message = "ID Number must be a non-negative integer!")
public int crewID;

@Embedded
@NotNull(message = "Crew member must be assigned to one of the listed ships!")
public Ship ship;


public String toString() {
return "Name: " + name + "<br>CrewID: " + crewID + "<br>Rank: " + rank;
return "Name: " + name + "<br>CrewID: " + crewID + "<br>Rank: " + rank + ship.shipName != null ? "<br>Ship: " + ship.shipName : "";

}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/openliberty/sample/application/CrewMembers.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*******************************************************************************
* Copyright (c) 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package io.openliberty.sample.application;

import java.util.List;
Expand All @@ -19,6 +29,8 @@ public interface CrewMembers extends DataRepository<CrewMember, Integer> {
List<CrewMember> findByRank(Rank rank);

Page<CrewMember> findByRank(Rank rank, Pageable pageRequest);

List<CrewMember> findByShipSizeAndRank(Ship.Size size, Rank rank);

@OrderBy("name")
Stream<CrewMember> findAll();
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/openliberty/sample/application/CrewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ public String retrieveByRank(@PathParam("rank") String rank,
return crewMembersToJsonArray(page);
}

@GET
@Path("/shipSize/{shipSize}/rank/{rank}")
public String retrieveByShipSizeAndRank(@PathParam("shipSize") Ship.Size size, @PathParam("rank") String rank) {
JsonArrayBuilder jab = Json.createArrayBuilder();
for (CrewMember c : crewMembers.findByShipSizeAndRank(size, Rank.fromString(rank))) {
JsonObject json = Json.createObjectBuilder()
.add("Name", c.name)
.add("CrewID", c.crewID)
.add("Ship", c.ship.shipName).build();
jab.add(json);
}
return jab.build().toString();
}

@DELETE
public void remove() {
crewMembers.deleteAll();
Expand All @@ -108,7 +122,8 @@ private String crewMembersToJsonArray(Iterable<CrewMember> crewMembers) {
JsonObject json = Json.createObjectBuilder()
.add("Name", c.name)
.add("CrewID", c.crewID)
.add("Rank",c.rank.toString()).build();
.add("Rank",c.rank.toString())
.add("Ship", c.ship.shipName).build();
jab.add(json);
}
return jab.build().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class RankDeserializer implements JsonbDeserializer<Rank> {

@Override
public Rank deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
String name = parser.getValue().toString().replaceAll("\"", "");
return Rank.fromString(name);
return Rank.fromString(parser.getString());
}

}
26 changes: 26 additions & 0 deletions src/main/java/io/openliberty/sample/application/Ship.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.openliberty.sample.application;

import jakarta.json.bind.annotation.JsonbTypeDeserializer;
import jakarta.persistence.Embeddable;

@Embeddable
@JsonbTypeDeserializer(ShipDeserializer.class)
public class Ship {
public static enum Size {
small, large
}

public String shipName;

public Size size;

Ship() {

}

Ship(String name, Size size) {
this.shipName = name;
this.size = size;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.openliberty.sample.application;

import java.lang.reflect.Type;

import jakarta.json.bind.serializer.DeserializationContext;
import jakarta.json.bind.serializer.JsonbDeserializer;
import jakarta.json.stream.JsonParser;

public class ShipDeserializer implements JsonbDeserializer<Ship> {

@Override
public Ship deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
String shipName = parser.getString();
if (shipName.equals("Liberty Saucer")) {
return new Ship(shipName, Ship.Size.small);
} else if (shipName.equals("Jakarta Sailboat")) {
return new Ship(shipName, Ship.Size.small);
} else if (shipName.equals("WebSphere Battleship")) {
return new Ship(shipName, Ship.Size.large);
} else {
return null;
}

}

}
2 changes: 1 addition & 1 deletion src/main/webapp/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ button:hover {
justify-content: space-between;
}

#findByRank, #findAll {
.queryContainer {
background-color: white;
margin: 1em;
padding: .5em;
Expand Down
25 changes: 25 additions & 0 deletions src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<title>Jakarta Data Sample</title>
<script type="text/javascript" src="js/find-all.js"></script>
<script type="text/javascript" src="js/find-by-rank.js"></script>
<script type="text/javascript" src="js/find-by-ship-size-and-rank.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<link href="https://fonts.googleapis.com/css?family=Asap" rel="stylesheet">
<link rel="stylesheet" href="css/app.css">
Expand Down Expand Up @@ -54,6 +55,14 @@ <h2>Congrats on your shiny, new Open Liberty sample app!</h2>
<option>Engineer</option>
</select>
</div>
<div id="Ship" class="vFlexContainer inputLine">
<span>Ship Assignment</span>
<select id="crewMemberShip">
<option>Liberty Saucer</option>
<option>Jakarta Sailboat</option>
<option>WebSphere Battleship</option>
</select>
</div>
<button id="registerButton" class="buttons" onclick="addCrewMember()">Register Crew Member</button>
</div>
</div>
Expand All @@ -62,6 +71,7 @@ <h2>Congrats on your shiny, new Open Liberty sample app!</h2>
<div id="queryButtons" class="vFlexContainer">
<button id="findAllButton" class="buttons queryButton selectedQuery" onclick="setActiveQuery('findAll')">findAll</button>
<button id="findByRankButton" class="buttons queryButton" onclick="setActiveQuery('findByRank')">findByRank</button>
<button id="findByShipSizeAndRankButton" class="buttons queryButton" onclick="setActiveQuery('findByShipSizeAndRank')">findByShipSizeAndRank</button>
</div>
</div>
<div id="findAll" class="vFlexContainer queryContainer" style="display: flex">
Expand All @@ -78,6 +88,21 @@ <h2>Congrats on your shiny, new Open Liberty sample app!</h2>
<div id="Engineer" class="flexBox"></div>
</div>
</div>
<div id="findByShipSizeAndRank" class="flexBox vFlexContainer queryContainer">
<strong id="createCrewMember" class="flexSelfCenter">Crew Members by Ship Size and Rank<br>CrewMembers.findByShipSizeAndRank()</strong>
<div id="findByShipSizeAndRankSelectors" class="hFlexContainer">
<select id="shipSizeSelector" onchange="refreshDisplay()">
<option>small</option>
<option>large</option>
</select>
<select id="rankSelector" onchange="refreshDisplay()">
<option>Captain</option>
<option>Officer</option>
<option>Engineer</option>
</select>
</div>
<div id="shipAndRankBoxes" class="flexBox hFlexContainer"></div>
</div>
</div>
</section>
</body>
Expand Down
11 changes: 9 additions & 2 deletions src/main/webapp/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function addCrewMember() {
var rank = document.getElementById("crewMemberRank");
crewMember.rank = rank.options[rank.selectedIndex].text;
crewMember.crewID = document.getElementById("crewMemberID").value;
crewMember.ship = document.getElementById("crewMemberShip").value


const response = await fetch("db/crew/"+crewMember.crewID, {
Expand Down Expand Up @@ -65,11 +66,17 @@ async function refreshDisplay() {

if (document.getElementById("findAll").style.display == 'flex') refreshFindAll();
if (document.getElementById("findByRank").style.display == 'flex') refreshFindByRank();

if (document.getElementById("findByShipSizeAndRank").style.display == 'flex') {
var shipSizeSelector = document.getElementById("shipSizeSelector");
var shipSize = shipSizeSelector.options[shipSizeSelector.selectedIndex].text;
var rankSelector = document.getElementById("rankSelector");
var rank = rankSelector.options[rankSelector.selectedIndex].text;
refreshFindByShipSizeAndRank(shipSize, rank);
}
}

function clearDisplay(){
var elements = ["userBoxes", "Captain", "Engineer", "Officer"];
var elements = ["userBoxes", "Captain", "Engineer", "Officer", "shipAndRankBoxes"]; //TODO this should use a .class
for (let i = 0; i < elements.length; i++) {
var usersDiv = document.getElementById(elements[i]);
while (usersDiv.firstChild) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/js/find-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ async function refreshFindAll() {
function addToCrewMembers(entry){
var userHtml = "<div>Name: " + entry.Name + "</div>" +
"<div>ID: " + entry.CrewID + "</div>" +
"<div>Rank: " + entry.Rank + "</div>";
"<div>Rank: " + entry.Rank + "</div>" +
"<div>Ship: " + entry.Ship + "</div>";

var userDiv = document.createElement("div");
userDiv.setAttribute("class","user flexbox");
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/js/find-by-rank.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ async function parseFindByRank(rank, response) {

function addToCrewMembersByRank(entry, rank) {
var userHtml = "<div>Name: " + entry.Name + "</div>" +
"<div>ID: " + entry.CrewID + "</div>";
"<div>ID: " + entry.CrewID + "</div>" +
"<div>Ship: " + entry.Ship + "</div>";

var userDiv = document.createElement("div");
userDiv.setAttribute("class","user flexbox");
Expand Down
32 changes: 32 additions & 0 deletions src/main/webapp/js/find-by-ship-size-and-rank.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

async function refreshFindByShipSizeAndRank(shipSize, rank) {
response = await fetch("db/crew/shipSize/" +shipSize+"/rank/" +rank);

if (response.ok) {
const doc = await response.json();
doc.forEach(addToCrewMembersFindByShipSizeAndRank);
}
}

function addToCrewMembersFindByShipSizeAndRank(entry){
var userHtml = "<div>Name: " + entry.Name + "</div>" +
"<div>ID: " + entry.CrewID + "</div>" +
"<div>Ship: " + entry.Ship + "</div>";

var userDiv = document.createElement("div");
userDiv.setAttribute("class","user flexbox");
userDiv.setAttribute("id",entry.CrewID);
userDiv.setAttribute("onclick","remove('"+entry.CrewID+"')");
userDiv.innerHTML=userHtml;
document.getElementById("shipAndRankBoxes").appendChild(userDiv);
}
Loading

0 comments on commit 8e5d288

Please sign in to comment.