Skip to content

Commit

Permalink
Merge pull request #3882 from scmacdon/main
Browse files Browse the repository at this point in the history
Update 2 examples
  • Loading branch information
scmacdon authored Oct 28, 2022
2 parents 7aa9bfb + cb5101a commit 88d052b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 25 deletions.
29 changes: 23 additions & 6 deletions javav2/usecases/CreatingSpringRedshiftRest/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ The following Java code represents the **WorkItemRepository** class. Notice that
In addition, notice the use of [Class SqlParameter](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/redshiftdata/model/SqlParameter.html) when using SQL statements. For example, in the **getData** method, you build a list of **SqlParameter** objects used to get records from the database.

```java

package com.aws.rest;

import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -484,14 +485,14 @@ public class WorkItemRepository {

// Get all records from the Amazon Redshift table.
if (arch.compareTo("") == 0) {
sqlStatement = "SELECT idwork, date, description, guide, status, username FROM work";
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive FROM work";
ExecuteStatementResponse response = executeAll(sqlStatement);
String id = response.id();
System.out.println("The identifier of the statement is "+id);
checkStatement(id);
return getResults(id);
} else {
sqlStatement = "SELECT idwork, date, description, guide, status, username " +
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive " +
"FROM work WHERE username = :username and archive = :arch ;";

parameters = List.of(
Expand Down Expand Up @@ -608,8 +609,6 @@ public class WorkItemRepository {
flipItemArchive(sqlStatement,parameters);
}



public String injectNewSubmission(WorkItem item) {
try {
String name = item.getName();
Expand Down Expand Up @@ -648,6 +647,7 @@ public class WorkItemRepository {
}
}


```

### WorkItem class
Expand All @@ -668,11 +668,17 @@ public class WorkItem {
private String date;
private String description;
private String status;
private boolean archived ;

public static WorkItem from(List<Field> fields) {
var item = new WorkItem();
for (int i = 0; i <= 5; i++) {
String value = fields.get(i).stringValue();
for (int i = 0; i <= 6; i++) {
String value="";
boolean val = false;
value = fields.get(i).stringValue();
if (i == 6)
val = fields.get(i).booleanValue();

switch (i) {
case 0:
item.setId(value);
Expand All @@ -692,11 +698,22 @@ public class WorkItem {
case 5:
item.setName(value);
break;
case 6:
item.setArchived(val);
break;
}
}
return item;
}

public boolean getArchived() {
return this.archived;
}

public void setArchived(boolean archived) {
this.archived = archived;
}

public void setId(String id) {
this.id = id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ public class WorkItem {
private String date;
private String description;
private String status;
private boolean archived ;

public static WorkItem from(List<Field> fields) {
var item = new WorkItem();
for (int i = 0; i <= 5; i++) {
String value = fields.get(i).stringValue();
for (int i = 0; i <= 6; i++) {
String value="";
boolean val = false;
value = fields.get(i).stringValue();
if (i == 6)
val = fields.get(i).booleanValue();

switch (i) {
case 0:
item.setId(value);
Expand All @@ -40,11 +46,22 @@ public static WorkItem from(List<Field> fields) {
case 5:
item.setName(value);
break;
case 6:
item.setArchived(val);
break;
}
}
return item;
}

public boolean getArchived() {
return this.archived;
}

public void setArchived(boolean archived) {
this.archived = archived;
}

public void setId(String id) {
this.id = id;
}
Expand Down Expand Up @@ -93,4 +110,4 @@ public void setGuide(String guide) {
public String getGuide() {
return this.guide;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public List<WorkItem> getData(String arch) {

// Get all records from the Amazon Redshift table.
if (arch.compareTo("") == 0) {
sqlStatement = "SELECT idwork, date, description, guide, status, username FROM work";
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive FROM work";
ExecuteStatementResponse response = executeAll(sqlStatement);
String id = response.id();
System.out.println("The identifier of the statement is "+id);
checkStatement(id);
return getResults(id);
} else {
sqlStatement = "SELECT idwork, date, description, guide, status, username " +
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive " +
"FROM work WHERE username = :username and archive = :arch ;";

parameters = List.of(
Expand Down
30 changes: 23 additions & 7 deletions javav2/usecases/Creating_Spring_RDS_ Rest/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public class WorkItemRepository implements CrudRepository<WorkItem, String> {

@Override
public Optional<WorkItem> findById(String s) {
String sqlStatement = "SELECT idwork, date, description, guide, status, username FROM work WHERE idwork = :id;";
String sqlStatement = "SELECT idwork, date, description, guide, status, username, archive FROM work WHERE idwork = :id;";
List<SqlParameter> parameters = List.of(param("id", s));
var result = execute(sqlStatement, parameters)
.records()
Expand Down Expand Up @@ -586,7 +586,7 @@ public class WorkItemRepository implements CrudRepository<WorkItem, String> {
param("id", id),
param("arch", archived)
);
execute(sqlStatement, parameters);
execute(sqlStatement, parameters);
} catch (RdsDataException e) {
e.printStackTrace();
}
Expand All @@ -598,7 +598,7 @@ public class WorkItemRepository implements CrudRepository<WorkItem, String> {
String isArc;

if (status.compareTo("true") == 0) {
sqlStatement = "SELECT idwork, date, description, guide, status, username " +
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive " +
"FROM work WHERE archive = :arch ;";
isArc = "1";
List<SqlParameter> parameters = List.of(
Expand All @@ -611,7 +611,7 @@ public class WorkItemRepository implements CrudRepository<WorkItem, String> {
.collect(Collectors.toUnmodifiableList());

} else if (status.compareTo("false") == 0) {
sqlStatement = "SELECT idwork, date, description, guide, status, username " +
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive " +
"FROM work WHERE archive = :arch ;";
isArc = "0";
List<SqlParameter> parameters = List.of(
Expand All @@ -624,7 +624,7 @@ public class WorkItemRepository implements CrudRepository<WorkItem, String> {
.collect(Collectors.toUnmodifiableList());

} else {
sqlStatement = "SELECT idwork, date, description, guide, status, username FROM work ;";
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive FROM work ;";
List<SqlParameter> parameters = List.of(

);
Expand Down Expand Up @@ -685,7 +685,6 @@ public class WorkItemRepository implements CrudRepository<WorkItem, String> {
}
}


```


Expand All @@ -707,11 +706,16 @@ public class WorkItem {
private String date;
private String description;
private String status;
private boolean archived;

public static WorkItem from(List<Field> fields) {
var item = new WorkItem();
for (int i = 0; i <= 5; i++) {
boolean arcVal = false;
for (int i = 0; i <= 6; i++) {
String value = fields.get(i).stringValue();
if (i == 6)
arcVal = fields.get(i).booleanValue();

switch (i) {
case 0:
item.setId(value);
Expand All @@ -731,11 +735,23 @@ public class WorkItem {
case 5:
item.setName(value);
break;

case 6:
item.setArchived(arcVal);
break;
}
}
return item;
}

public void setArchived(boolean archived) {
this.archived =archived;
}

public boolean getArchived() {
return this.archived;
}

public void setId(String id) {
this.id = id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ public class WorkItem {
private String date;
private String description;
private String status;
private boolean archived;

public static WorkItem from(List<Field> fields) {
var item = new WorkItem();
for (int i = 0; i <= 5; i++) {
boolean arcVal = false;
for (int i = 0; i <= 6; i++) {
String value = fields.get(i).stringValue();

if (i == 6)
arcVal = fields.get(i).booleanValue();

switch (i) {
case 0:
item.setId(value);
Expand All @@ -40,11 +46,23 @@ public static WorkItem from(List<Field> fields) {
case 5:
item.setName(value);
break;

case 6:
item.setArchived(arcVal);
break;
}
}
return item;
}

public void setArchived(boolean archived) {
this.archived =archived;
}

public boolean getArchived() {
return this.archived;
}

public void setId(String id) {
this.id = id;
}
Expand Down Expand Up @@ -93,4 +111,4 @@ public void setGuide(String guide) {
public String getGuide() {
return this.guide;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public <S extends WorkItem> Iterable<S> saveAll(Iterable<S> entities) {

@Override
public Optional<WorkItem> findById(String s) {
String sqlStatement = "SELECT idwork, date, description, guide, status, username FROM work WHERE idwork = :id;";
String sqlStatement = "SELECT idwork, date, description, guide, status, username, archive FROM work WHERE idwork = :id;";
List<SqlParameter> parameters = List.of(param("id", s));
var result = execute(sqlStatement, parameters)
.records()
Expand Down Expand Up @@ -129,7 +129,7 @@ public void flipItemArchive(String id) {
param("id", id),
param("arch", archived)
);
execute(sqlStatement, parameters);
execute(sqlStatement, parameters);
} catch (RdsDataException e) {
e.printStackTrace();
}
Expand All @@ -141,7 +141,7 @@ public Iterable<WorkItem> findAllWithStatus(String status) {
String isArc;

if (status.compareTo("true") == 0) {
sqlStatement = "SELECT idwork, date, description, guide, status, username " +
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive " +
"FROM work WHERE archive = :arch ;";
isArc = "1";
List<SqlParameter> parameters = List.of(
Expand All @@ -154,7 +154,7 @@ public Iterable<WorkItem> findAllWithStatus(String status) {
.collect(Collectors.toUnmodifiableList());

} else if (status.compareTo("false") == 0) {
sqlStatement = "SELECT idwork, date, description, guide, status, username " +
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive " +
"FROM work WHERE archive = :arch ;";
isArc = "0";
List<SqlParameter> parameters = List.of(
Expand All @@ -167,7 +167,7 @@ public Iterable<WorkItem> findAllWithStatus(String status) {
.collect(Collectors.toUnmodifiableList());

} else {
sqlStatement = "SELECT idwork, date, description, guide, status, username FROM work ;";
sqlStatement = "SELECT idwork, date, description, guide, status, username, archive FROM work ;";
List<SqlParameter> parameters = List.of(

);
Expand Down

0 comments on commit 88d052b

Please sign in to comment.