|
| 1 | +package org.labkey.remoteapi.test; |
| 2 | + |
| 3 | +import org.labkey.remoteapi.ApiKeyCredentialsProvider; |
| 4 | +import org.labkey.remoteapi.Connection; |
| 5 | +import org.labkey.remoteapi.domain.CreateDomainCommand; |
| 6 | +import org.labkey.remoteapi.domain.Domain; |
| 7 | +import org.labkey.remoteapi.domain.PropertyDescriptor; |
| 8 | +import org.labkey.remoteapi.query.InsertRowsCommand; |
| 9 | +import org.labkey.remoteapi.query.QuerySaveRowsCommand; |
| 10 | +import org.labkey.remoteapi.query.QuerySaveRowsCommand.Command; |
| 11 | +import org.labkey.remoteapi.query.QuerySaveRowsCommand.CommandType; |
| 12 | +import org.labkey.remoteapi.query.QuerySaveRowsResponse; |
| 13 | +import org.labkey.remoteapi.query.SaveRowsCommand; |
| 14 | +import org.labkey.remoteapi.query.SaveRowsResponse; |
| 15 | +import org.labkey.remoteapi.security.CreateContainerCommand; |
| 16 | +import org.labkey.remoteapi.security.DeleteContainerCommand; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +public class QuerySaveRowsCommandDemo |
| 22 | +{ |
| 23 | + public static void main(String[] args) throws Exception |
| 24 | + { |
| 25 | + String folderPath = "SaveRowsCommandDemo"; |
| 26 | + String schemaName = "lists"; |
| 27 | + String queryName = "Players"; |
| 28 | + |
| 29 | + ApiKeyCredentialsProvider credentials = new ApiKeyCredentialsProvider("xxx"); |
| 30 | + Connection conn = new Connection("http://localhost:8080", credentials); |
| 31 | + |
| 32 | + // Create the project |
| 33 | + new CreateContainerCommand(folderPath).execute(conn, "/"); |
| 34 | + |
| 35 | + try |
| 36 | + { |
| 37 | + // Create a list |
| 38 | + { |
| 39 | + CreateDomainCommand createdListCmd = new CreateDomainCommand("IntList", queryName); |
| 40 | + createdListCmd.setOptions(Map.of("keyName", "JerseyNumber", "keyType", "Integer")); |
| 41 | + |
| 42 | + Domain domain = createdListCmd.getDomainDesign(); |
| 43 | + domain.setFields(List.of( |
| 44 | + new PropertyDescriptor("FirstName", "First Name", "string"), |
| 45 | + new PropertyDescriptor("LastName", "Last Name", "string"), |
| 46 | + new PropertyDescriptor("Team", null, "string") |
| 47 | + )); |
| 48 | + |
| 49 | + createdListCmd.execute(conn, folderPath); |
| 50 | + } |
| 51 | + |
| 52 | + // Add some initial data |
| 53 | + { |
| 54 | + InsertRowsCommand insertCmd = new InsertRowsCommand(schemaName, queryName); |
| 55 | + insertCmd.addRow(Map.of("FirstName", "Alvin", "LastName", "David", "JerseyNumber", 21, "Team", "Seattle Mariners")); |
| 56 | + insertCmd.addRow(Map.of("FirstName", "Jay", "LastName", "Buhner", "JerseyNumber", 19, "Team", "New York Yankees")); |
| 57 | + insertCmd.addRow(Map.of("FirstName", "Ken", "LastName", "Phelps", "JerseyNumber", 44, "Team", "Seattle Mariners")); |
| 58 | + |
| 59 | + SaveRowsResponse response = insertCmd.execute(conn, folderPath); |
| 60 | + System.out.printf(String.format("Inserted %d players%n", response.getRowsAffected().intValue())); |
| 61 | + } |
| 62 | + |
| 63 | + // Execute multiple query operations using a saveRows command |
| 64 | + { |
| 65 | + QuerySaveRowsCommand saveCmd = new QuerySaveRowsCommand(); |
| 66 | + |
| 67 | + // Draft Ken Griffey Jr. |
| 68 | + saveCmd.addCommand(new Command(CommandType.Insert, schemaName, queryName, List.of(Map.of("FirstName", "Ken", "LastName", "Griffey Jr.", "JerseyNumber", 24, "Team", "Seattle Mariners")))); |
| 69 | + |
| 70 | + // Trade for Jay Buhner |
| 71 | + Command tradeJayBuhnerCommand = new Command(CommandType.Update, schemaName, queryName, List.of( |
| 72 | + Map.of("JerseyNumber", 19, "Team", "Seattle Mariners"), |
| 73 | + Map.of("JerseyNumber", 44, "Team", "New York Yankees") |
| 74 | + )); |
| 75 | + tradeJayBuhnerCommand.setAuditBehavior(SaveRowsCommand.AuditBehavior.DETAILED); |
| 76 | + tradeJayBuhnerCommand.setAuditUserComment("Traded Jay Buhner for Ken Phelps on July 21, 1988"); |
| 77 | + saveCmd.addCommand(tradeJayBuhnerCommand); |
| 78 | + |
| 79 | + // Alvin Davis retires |
| 80 | + saveCmd.addCommand(new Command(CommandType.Delete, schemaName, queryName, List.of(Map.of("JerseyNumber", 21)))); |
| 81 | + |
| 82 | + QuerySaveRowsResponse response = saveCmd.execute(conn, folderPath); |
| 83 | + System.out.printf("Executed saveRows command with %d errors and %d results%n", response.getErrorCount(), response.getResults().size()); |
| 84 | + } |
| 85 | + } |
| 86 | + catch (Exception e) |
| 87 | + { |
| 88 | + System.out.printf("Error: %s%n", e.getMessage()); |
| 89 | + System.out.printf("Type: %s%n", e.getClass().getName()); |
| 90 | + System.out.println("Stack trace:"); |
| 91 | + for (StackTraceElement element : e.getStackTrace()) |
| 92 | + System.out.printf(" at %s%n", element.toString()); |
| 93 | + } |
| 94 | + finally |
| 95 | + { |
| 96 | + new DeleteContainerCommand().execute(conn, folderPath); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments