Skip to content

Commit 11cb97c

Browse files
author
Joerg Huber
committed
Example code on how to use Named Query Functionality.
1 parent f093455 commit 11cb97c

3 files changed

Lines changed: 290 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package systemic.sif3.demo.rest.consumer.namedquery;
2+
3+
import sif3.common.model.PagingInfo;
4+
import sif3.common.model.StringPayload;
5+
import sif3.common.model.delayed.DelayedResponseReceipt;
6+
import sif3.common.ws.ErrorDetails;
7+
import sif3.infra.rest.consumer.AbstractNamedQueryConsumer;
8+
9+
public class StudentsInYearConsumer extends AbstractNamedQueryConsumer
10+
{
11+
public StudentsInYearConsumer()
12+
{
13+
super();
14+
}
15+
16+
@Override
17+
public String getNamedQueryName()
18+
{
19+
return "StudentsInYear"; // Name of named query as it appears in the environment XML.
20+
}
21+
22+
@Override
23+
public void processDelayedNamedQuery(StringPayload responseData, PagingInfo pagingInfo, DelayedResponseReceipt receipt)
24+
{
25+
logger.debug("Received DELAYED NAMED QUERY Response:\nPagingInfo:\n"+pagingInfo+"\nDelayed Receipt Details:\n"+receipt+"\nResponse Data:\n"+responseData);
26+
}
27+
28+
@Override
29+
public void processDelayedError(ErrorDetails error, DelayedResponseReceipt receipt)
30+
{
31+
logger.debug("Received DELAYED ERROR Response:\n"+error+"\nDelayed Receipt Details:\n"+receipt);
32+
}
33+
34+
@Override
35+
public void shutdown()
36+
{
37+
logger.info("Shutting down Named Query Service Consumer '"+getNamedQueryName()+"'");
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* StudentsInYear.java
3+
* Created: 4 Dec 2018
4+
*
5+
* Copyright 2018 Systemic Pty Ltd
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8+
* in compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed under the License
13+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14+
* or implied. See the License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package systemic.sif3.demo.rest.provider.namedquery;
19+
20+
import javax.ws.rs.core.MediaType;
21+
22+
import sif3.common.exception.DataTooLargeException;
23+
import sif3.common.exception.PersistenceException;
24+
import sif3.common.exception.SIFException;
25+
import sif3.common.exception.UnsupportedMediaTypeExcpetion;
26+
import sif3.common.model.PagingInfo;
27+
import sif3.common.model.QueryTemplateInfo;
28+
import sif3.common.model.RequestMetadata;
29+
import sif3.common.model.ResponseParameters;
30+
import sif3.common.model.SIFContext;
31+
import sif3.common.model.SIFZone;
32+
import sif3.common.model.StringPayload;
33+
import sif3.infra.rest.provider.namedquery.BaseNamedQueryProvider;
34+
35+
/**
36+
* @author Joerg Huber
37+
*
38+
*/
39+
public class StudentsInYearProvider extends BaseNamedQueryProvider
40+
{
41+
42+
/*
43+
* @see sif3.common.interfaces.NamedQueryProvider#retrieveData(sif3.common.model.QueryTemplateInfo, sif3.common.model.SIFZone, sif3.common.model.SIFContext, sif3.common.model.PagingInfo, sif3.common.model.RequestMetadata, sif3.common.model.ResponseParameters, javax.ws.rs.core.MediaType)
44+
*/
45+
@Override
46+
public StringPayload retrieveData(QueryTemplateInfo queryTemplateInfo,
47+
SIFZone zone,
48+
SIFContext context,
49+
PagingInfo pagingInfo,
50+
RequestMetadata metadata,
51+
ResponseParameters customResponseParams,
52+
MediaType returnMimeType)
53+
throws PersistenceException, UnsupportedMediaTypeExcpetion, DataTooLargeException, SIFException
54+
{
55+
logger.debug("StudentsInYearProvider.retrieveData request received with QueryTemplateInfo:\n"+queryTemplateInfo);
56+
if(returnMimeType.isCompatible(MediaType.APPLICATION_JSON_TYPE))
57+
{
58+
throw new UnsupportedMediaTypeExcpetion("JSON not supported for "+getServiceName()+" named query.");
59+
}
60+
StringPayload response = new StringPayload();
61+
response.setMimeType(returnMimeType);
62+
63+
response.setData(
64+
"<Students xmlns=\"http://www.au/StudentInYear/3.4.2\">\n"+
65+
" <school>4001</school>\n"+
66+
" <Student>\n"+
67+
" <LogonId>dan.petersen</LogonId>\n"+
68+
" </Student>\n"+
69+
" <Student>\n"+
70+
" <LogonId>leila.stone</LogonId>\n"+
71+
" </Student>\n"+
72+
"</Students>");
73+
74+
return response;
75+
}
76+
77+
/* Name of the service as it appears in the environment or the end-point.
78+
*
79+
* (non-Javadoc)
80+
* @see sif3.infra.rest.provider.CoreProvider#getServiceName()
81+
*/
82+
@Override
83+
public String getServiceName()
84+
{
85+
return "StudentsInYear";
86+
}
87+
88+
/* (non-Javadoc)
89+
* @see sif3.infra.rest.provider.CoreProvider#shutdown()
90+
*/
91+
@Override
92+
public void shutdown()
93+
{
94+
logger.debug("Shutdown Named Query Service for " + getPrettyName());
95+
}
96+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package sif3.test.infra.rest.consumer;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
7+
import javax.ws.rs.core.MediaType;
8+
9+
import sif3.common.header.HeaderValues.QueryIntention;
10+
import sif3.common.header.HeaderValues.RequestType;
11+
import sif3.common.model.PagingInfo;
12+
import sif3.common.model.SIFContext;
13+
import sif3.common.model.SIFZone;
14+
import sif3.common.model.ZoneContextInfo;
15+
import sif3.common.ws.Response;
16+
import sif3.infra.common.env.mgr.ConsumerEnvironmentManager;
17+
import sif3.infra.rest.consumer.ConsumerLoader;
18+
import systemic.sif3.demo.rest.consumer.namedquery.StudentsInYearConsumer;
19+
20+
public class TestStudentsInYearConsumer
21+
{
22+
private static final String CONSUMER_ID = "StudentConsumer";
23+
private static final RequestType REQUEST_TYPE = RequestType.IMMEDIATE;
24+
25+
private StudentsInYearConsumer getConsumer()
26+
{
27+
return new StudentsInYearConsumer();
28+
}
29+
30+
private void printResponses(List<Response> responses)
31+
{
32+
if (responses != null)
33+
{
34+
for (Response response : responses)
35+
{
36+
printResponse(response);
37+
}
38+
}
39+
else
40+
{
41+
System.out.println("Responses from attempt to execute a Named Query Service: null");
42+
}
43+
}
44+
45+
private void printResponse(Response response)
46+
{
47+
try
48+
{
49+
if (response != null)
50+
{
51+
System.out.println("Response:\n"+response);
52+
if (response.hasError())
53+
{
54+
System.out.println("Error for Response: "+response.getError());
55+
}
56+
else // We may have data
57+
{
58+
if (response.getHasEntity())
59+
{
60+
// Check if response type is String
61+
if (response.getDataObjectType() != String.class) // This is for phase response
62+
{
63+
System.out.println("Unexpectded Data Type retrieved in Response ("+response.getDataObjectType()+")! Cannot unmarshal");
64+
}
65+
}
66+
else // in delayed we may have delayed receipt
67+
{
68+
System.out.println("No Data Returned. Response Status = "+response.getStatus()+" ("+response.getStatusMessage()+")");
69+
System.out.println("Delayed Receipt: "+response.getDelayedReceipt());
70+
}
71+
}
72+
}
73+
else
74+
{
75+
System.out.println("Responses from attempt to execute a Named Query Service: null");
76+
}
77+
}
78+
catch (Exception ex)
79+
{
80+
ex.printStackTrace();
81+
}
82+
}
83+
84+
private void getStudentsInYear(StudentsInYearConsumer consumer)
85+
{
86+
System.out.println("Start 'Get Students In Year' ...");
87+
try
88+
{
89+
List<ZoneContextInfo> zoneCtxList = new ArrayList<ZoneContextInfo>();
90+
zoneCtxList.add(new ZoneContextInfo((SIFZone)null, (SIFContext)null)); // Default zone and context
91+
// zoneCtxList.add(new ZoneContextInfo(new SIFZone("auRolloverTestingZone"), null));
92+
93+
HashMap<String, String> namedQueryParameters = new HashMap<String, String>();
94+
namedQueryParameters.put("Year", "Yr7");
95+
namedQueryParameters.put("SchoolCode", "4001");
96+
97+
List<Response> responses = consumer.retrieveDataFromNamedQuery(namedQueryParameters, MediaType.APPLICATION_XML_TYPE, new PagingInfo(5, 1), zoneCtxList, REQUEST_TYPE, QueryIntention.ALL, null);
98+
99+
System.out.println("Responses from attempt to Get Job:");
100+
printResponses(responses);
101+
}
102+
catch (Exception ex)
103+
{
104+
ex.printStackTrace();
105+
}
106+
System.out.println("Finished 'Get Students in Year'.");
107+
}
108+
109+
110+
public static void main(String[] args)
111+
{
112+
TestStudentsInYearConsumer tester = new TestStudentsInYearConsumer();
113+
System.out.println("Start Testing StudentsInYearConsumer...");
114+
115+
if (ConsumerLoader.initialise(CONSUMER_ID))
116+
{
117+
System.out.println("Consumer loaded successfully. Environment Data:\n"+ConsumerEnvironmentManager.getInstance().getEnvironmentInfo());
118+
119+
StudentsInYearConsumer consumer = tester.getConsumer();
120+
StudentsInYearConsumer consumer2 = tester.getConsumer();
121+
122+
//
123+
// Get Students
124+
//
125+
tester.getStudentsInYear(consumer);
126+
tester.getStudentsInYear(consumer2);
127+
128+
// Put this agent to a blocking wait.....
129+
if (true)
130+
{
131+
try
132+
{
133+
Object semaphore = new Object();
134+
synchronized (semaphore) // this will block until CTRL-C is pressed.
135+
{
136+
System.out.println("==================================================\nTestStudentsInYearConsumer is running (Press Ctrl-C to stop)\n==================================================");
137+
semaphore.wait();
138+
}
139+
}
140+
catch (Exception ex)
141+
{
142+
System.out.println("Blocking wait in TestStudentsInYearConsumer interrupted: " + ex.getMessage());
143+
ex.printStackTrace();
144+
}
145+
}
146+
147+
System.out.println("Finalise Consumer (i.e. disconnect and remove environment).");
148+
consumer.finalise();
149+
}
150+
151+
ConsumerLoader.shutdown();
152+
System.out.println("End Testing StudentsInYearConsumer.");
153+
}
154+
}

0 commit comments

Comments
 (0)