Skip to content

Commit 51bb289

Browse files
initial commit of quickstart-supporting code (#234)
1 parent 6f9f45f commit 51bb289

21 files changed

+1815
-0
lines changed

samples/quickstart/app/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Copyright (c) 2020, 2023, Oracle and/or its affiliates. -->
3+
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->
4+
<project xmlns="http://maven.apache.org/POM/4.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
7+
<modelVersion>4.0.0</modelVersion>
8+
9+
<groupId>com.oracle.weblogic.example</groupId>
10+
<artifactId>todo</artifactId>
11+
<version>0.1-SNAPSHOT</version>
12+
<packaging>war</packaging>
13+
<name>todo</name>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>javax.ws.rs</groupId>
24+
<artifactId>javax.ws.rs-api</artifactId>
25+
<version>2.1.1</version>
26+
<scope>provided</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>javax.json</groupId>
30+
<artifactId>javax.json-api</artifactId>
31+
<version>1.1.4</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<finalName>todo</finalName>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-enforcer-plugin</artifactId>
42+
<version>3.2.1</version>
43+
<executions>
44+
<execution>
45+
<id>enforce-build-environment</id>
46+
<goals>
47+
<goal>enforce</goal>
48+
</goals>
49+
<configuration>
50+
<rules>
51+
<requireJavaVersion>
52+
<version>1.8.0</version>
53+
<message>You must use JDK 8 to build the project WebLogic 12.2.1.4 only supports JDK 8</message>
54+
</requireJavaVersion>
55+
</rules>
56+
</configuration>
57+
</execution>
58+
</executions>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<version>3.11.0</version>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-war-plugin</artifactId>
68+
<version>3.3.2</version>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package org.todo.services;
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
import javax.ws.rs.ApplicationPath;
9+
import javax.ws.rs.core.Application;
10+
11+
import org.todo.services.resource.ItemResource;
12+
import org.todo.services.resource.ItemsResource;
13+
14+
@ApplicationPath("/rest")
15+
public class TodoListApplication extends Application {
16+
public Set<Class<?>> getClasses() {
17+
Set<Class<?>> s = new HashSet<>();
18+
s.add(ItemsResource.class);
19+
s.add(ItemResource.class);
20+
return s;
21+
}
22+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package org.todo.services.entity;
5+
6+
import javax.json.Json;
7+
import javax.json.JsonObject;
8+
9+
public class Item {
10+
private int key;
11+
private String description;
12+
private boolean complete;
13+
14+
public Item(int id) {
15+
key = id;
16+
complete = false;
17+
}
18+
19+
public int id() {
20+
return key;
21+
}
22+
23+
public String desc() {
24+
return description;
25+
}
26+
27+
public Item desc(String value) {
28+
description = value;
29+
return this;
30+
}
31+
32+
public boolean done() {
33+
return complete;
34+
}
35+
36+
public Item done(boolean value) {
37+
complete = value;
38+
return this;
39+
}
40+
41+
public JsonObject toJson() {
42+
return Json.createObjectBuilder()
43+
.add("id", id())
44+
.add( "description", desc())
45+
.add( "done", done())
46+
.build();
47+
}
48+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package org.todo.services.resource;
5+
6+
import java.sql.Connection;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
import javax.json.JsonObject;
11+
import javax.naming.NamingException;
12+
import javax.ws.rs.DELETE;
13+
import javax.ws.rs.GET;
14+
import javax.ws.rs.PUT;
15+
import javax.ws.rs.Path;
16+
import javax.ws.rs.PathParam;
17+
import javax.ws.rs.Produces;
18+
19+
import org.todo.services.entity.Item;
20+
21+
@Path("/item")
22+
public class ItemResource {
23+
24+
public final static String selectSql = "select task, completed from ToDos where taskId = %s";
25+
public final static String deleteSql = "delete from ToDos where taskId = %s";
26+
public final static String insertSql = "insert into ToDos(task, completed) values('%s', false);";
27+
public final static String updateSql = "update ToDos set completed = %s where taskId = %s";
28+
29+
@GET
30+
@Path("/{id}")
31+
@Produces("application/json")
32+
public JsonObject item(@PathParam("id") String id) {
33+
Item result = null;
34+
try (Connection conn = ItemsResource.datasource().getConnection()) {
35+
Statement statement = conn.createStatement();
36+
String queryStr = String.format(selectSql, id);
37+
System.out.println(queryStr);
38+
ResultSet resultSet = statement.executeQuery(queryStr);
39+
if (resultSet.next()) {
40+
String task = resultSet.getString("task");
41+
boolean complete = resultSet.getBoolean("completed");
42+
result = new Item(Integer.parseInt(id)).desc(task).done(complete);
43+
}
44+
} catch (SQLException | NamingException ex) {
45+
ex.printStackTrace();
46+
}
47+
return result == null ? null : result.toJson();
48+
}
49+
50+
@DELETE
51+
@Path("/{id}")
52+
public void delete(@PathParam("id") String id) {
53+
runQuery(String.format(deleteSql, id));
54+
}
55+
56+
@PUT
57+
@Path("/{taskDescription}")
58+
public void addNewItem(@PathParam("taskDescription") String description) {
59+
runQuery(String.format(insertSql, description));
60+
}
61+
62+
@PUT
63+
@Path("/{id}/{status}")
64+
public void updateStatus(@PathParam("id") String id, @PathParam("status") String status) {
65+
runQuery(String.format(updateSql, id, status));
66+
}
67+
68+
private void runQuery(String query) {
69+
try (Connection conn = ItemsResource.datasource().getConnection()) {
70+
Statement statement = conn.createStatement();
71+
System.out.println(query);
72+
statement.executeUpdate(query);
73+
} catch (SQLException | NamingException ex) {
74+
ex.printStackTrace();
75+
}
76+
}
77+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) 2020, 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package org.todo.services.resource;
5+
6+
import java.sql.Connection;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import javax.json.Json;
13+
import javax.json.JsonArray;
14+
import javax.json.JsonArrayBuilder;
15+
import javax.naming.InitialContext;
16+
import javax.naming.NamingException;
17+
import javax.sql.DataSource;
18+
import javax.ws.rs.GET;
19+
import javax.ws.rs.Path;
20+
import javax.ws.rs.Produces;
21+
import javax.ws.rs.core.MediaType;
22+
import javax.ws.rs.core.Response;
23+
24+
import org.todo.services.entity.Item;
25+
26+
/**
27+
* REST service for the To-Do list application using MySQL DB.
28+
* /items retrieves the full list of tasks
29+
* /items/init drops the table, creates the table, and loads the table with some starting tasks
30+
*/
31+
@Path("/items/")
32+
@Produces(MediaType.APPLICATION_JSON)
33+
public class ItemsResource {
34+
35+
public static DataSource datasource() throws NamingException {
36+
InitialContext ctx = new InitialContext();
37+
return (DataSource) ctx.lookup("jdbc/ToDoDB");
38+
}
39+
40+
public List<Item> items() {
41+
List<Item> result = new ArrayList<>();
42+
43+
try (Connection conn = datasource().getConnection()){
44+
Statement statement = conn.createStatement();
45+
ResultSet resultSet = statement.executeQuery("select taskId, task, completed from ToDos");
46+
while (resultSet.next()) {
47+
int id = resultSet.getInt("taskId");
48+
String task = resultSet.getString("task");
49+
boolean complete = resultSet.getBoolean("completed");
50+
result.add(new Item(id).desc(task).done(complete));
51+
}
52+
} catch (SQLException | NamingException ex) {
53+
ex.printStackTrace();
54+
}
55+
return result;
56+
}
57+
58+
@GET
59+
public JsonArray itemsJson() {
60+
JsonArrayBuilder result = Json.createArrayBuilder();
61+
for (Item item : items()) {
62+
result.add(item.toJson());
63+
}
64+
return result.build();
65+
}
66+
67+
@GET
68+
@Path("/drop/")
69+
@Produces(MediaType.TEXT_PLAIN)
70+
public Response dropTable() {
71+
try (Connection conn = datasource().getConnection()) {
72+
Statement stmt = conn.createStatement();
73+
74+
String dropTable = "drop table ToDos;";
75+
System.out.println(dropTable);
76+
stmt.executeUpdate(dropTable);
77+
} catch (SQLException | NamingException ex) {
78+
// ok to fail, table may not exist yet.
79+
return Response.ok().entity(ex.getLocalizedMessage() + "\n").build();
80+
}
81+
return Response.ok().entity("ToDos table dropped.\n").build();
82+
}
83+
84+
@GET
85+
@Path("/init/")
86+
@Produces(MediaType.TEXT_PLAIN)
87+
public Response initTable() {
88+
dropTable();
89+
try (Connection conn = datasource().getConnection()){
90+
Statement stmt = conn.createStatement();
91+
92+
String createTable = "create table ToDos (" +
93+
"taskId INT NOT NULL AUTO_INCREMENT, " +
94+
"task VARCHAR(200) NOT NULL, " +
95+
"completed BOOLEAN," +
96+
"constraint todo_pk PRIMARY KEY (taskId));";
97+
98+
System.out.println(createTable);
99+
stmt.executeUpdate(createTable);
100+
101+
String[] tasks = {"Install Verrazzano", "Move ToDo List to the cloud", "Celebrate", "Clean off my desk"};
102+
for (String task : tasks) {
103+
String insert = String.format(ItemResource.insertSql, task);
104+
System.out.println(insert);
105+
stmt.executeUpdate(insert);
106+
}
107+
108+
} catch (SQLException | NamingException ex) {
109+
ex.printStackTrace();
110+
return Response.serverError().entity("ERROR: " + ex.getLocalizedMessage() + "\n").build();
111+
}
112+
return Response.ok().entity("ToDos table initialized.\n").build();
113+
}
114+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Copyright (c) 2020, 2023, Oracle and/or its affiliates. -->
3+
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->
4+
5+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
6+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
8+
version="4.0">
9+
<display-name>Derek's ToDo List</display-name>
10+
<welcome-file-list>
11+
<welcome-file>index.html</welcome-file>
12+
</welcome-file-list>
13+
</web-app>

0 commit comments

Comments
 (0)