213 lines
7.6 KiB
HTML
213 lines
7.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="google-site-verification" content="CPhbjooaiTdROm7Vs4E7kuHZvBfkeLUtonGgcVUbTL8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="description" content="">
|
|
<meta name="author" content="">
|
|
<link rel="shortcut icon" href="ico/favicon.ico">
|
|
|
|
<title>Strolch: Services and Commands</title>
|
|
|
|
<!-- Bootstrap core CSS -->
|
|
<link href="css/bootstrap.min.css" rel="stylesheet">
|
|
|
|
<!-- Custom styles for this template -->
|
|
<link href="css/custom.css" rel="stylesheet">
|
|
|
|
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --><!--[if lt IE 9]>
|
|
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
|
|
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script><![endif]-->
|
|
</head>
|
|
<body>
|
|
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
|
|
<div class="container">
|
|
<div class="navbar-header">
|
|
<a class="navbar-brand" href="index.html">Strolch</a>
|
|
</div>
|
|
<div class="collapse navbar-collapse">
|
|
<ul class="nav navbar-nav">
|
|
<li><a href="index.html">Overview</a></li>
|
|
<li><a href="api.html">API</a></li>
|
|
<li class="active"><a href="documentation.html">Documentation</a></li>
|
|
<li><a href="tutorial.html">Tutorial</a></li>
|
|
<li><a href="downloads.html">Downloads</a></li>
|
|
<li><a href="development.html">Development</a></li>
|
|
<li><a href="blog.html">Blog</a></li>
|
|
</ul>
|
|
</div>
|
|
<!--/.nav-collapse -->
|
|
</div>
|
|
</div>
|
|
|
|
<div class="container">
|
|
|
|
<div class="page-header">
|
|
<h1 class="page-title">Documentation: Services and Commands</h1>
|
|
|
|
<p class="lead page-description">This page discusses Strolch's Services and Commands API</p>
|
|
</div>
|
|
|
|
<div class="content">
|
|
|
|
<p><code>Services</code> are written to implement a specific use-case. <code>Commands</code> are written to
|
|
implemente re-usable parts of a use-case. The use-case can be abstract e.g. <code>AddResourceService</code>
|
|
or very specific e.g. <code>CreatePatientService</code>.</p>
|
|
|
|
<p>Should the use-case be re-usable in different scenarios, then commands should implement the logic, and the
|
|
services should then execute the commands. E.g. The <code>CreatePatientService</code> would use a <code>CreatePatientResourceCommand</code>
|
|
and then use an <code>AddResourceCommand</code> in a single transaction, so that the task of creating the
|
|
actual Patient Resource can be re-used somewhere else.</p>
|
|
|
|
<p>Services extend the abstract class <code>AbstractService</code> and then implement the method <code>internalDoService(ServiceArgument)</code>.
|
|
AbstractService defines generic template arguments with which the concrete service can define a specific
|
|
input ServiceArgument class and output ServiceResult class.</p>
|
|
|
|
<p>The AbstractService class has multiple helper methods:</p>
|
|
<ul>
|
|
<li><code>openTx():StrolchTransaction</code> - to open a transaction</li>
|
|
<li><code>runPrivileged()</code> - to perform a SystemUserAction</li>
|
|
<li><code>getComponent():V</code> - to retrieve a specific StrolchComponent</li>
|
|
</ul>
|
|
<p>there are more - check the JavaDocs</p>
|
|
|
|
|
|
<p>Commands extend the <code>Command</code> class and then implement the method <code>doCommand()</code>.
|
|
Commands have helper methods:</p>
|
|
<ul>
|
|
<li><code>tx()</code> - to get the current transaction</li>
|
|
<li><code>getPolicy()</code> - to retrieve a policy instance</li>
|
|
<li><code>runPrivileged()</code> - to perform a SystemUserAction</li>
|
|
</ul>
|
|
<p>there are more - check the JavaDocs</p>
|
|
|
|
<p>The following code snippets shows how a Service and Command are used to perform the task of adding a new
|
|
Order. Note how:</p>
|
|
<ul>
|
|
<li>the Service opens the transaction</li>
|
|
<li>adds the command to the TX</li>
|
|
<li>calls <code>tx.commitOnClose()</code></li>
|
|
<li>the command validates its input</li>
|
|
<li>locks the object</li>
|
|
<li>performs the work</li>
|
|
<li>and implements an undo</li>
|
|
</ul>
|
|
|
|
<p>AddOrderService:</p>
|
|
<pre>
|
|
public class AddOrderService extends AbstractService<AddOrderService.AddOrderArg, ServiceResult> {
|
|
|
|
@Override
|
|
protected ServiceResult getResultInstance() {
|
|
return new ServiceResult();
|
|
}
|
|
|
|
@Override
|
|
protected ServiceResult internalDoService(AddOrderArg arg) {
|
|
|
|
try (StrolchTransaction tx = openTx(arg.realm)) {
|
|
AddOrderCommand command = new AddOrderCommand(getContainer(), tx);
|
|
command.setOrder(arg.order);
|
|
tx.addCommand(command);
|
|
tx.commitOnClose();
|
|
}
|
|
|
|
return ServiceResult.success();
|
|
}
|
|
|
|
public static class AddOrderArg extends ServiceArgument {
|
|
public Order order;
|
|
}
|
|
}
|
|
</pre>
|
|
|
|
<p>AddOrderCommand:</p>
|
|
<pre>
|
|
public class AddOrderCommand extends Command {
|
|
|
|
private Order order;
|
|
|
|
public AddOrderCommand(ComponentContainer container, StrolchTransaction tx) {
|
|
super(container, tx);
|
|
}
|
|
|
|
public void setOrder(Order order) {
|
|
this.order = order;
|
|
}
|
|
|
|
@Override
|
|
public void validate() {
|
|
DBC.PRE.assertNotNull("Order may not be null!", this.order);
|
|
}
|
|
|
|
@Override
|
|
public void doCommand() {
|
|
|
|
tx().lock(this.order);
|
|
|
|
OrderMap orderMap = tx().getOrderMap();
|
|
if (orderMap.hasElement(tx(), this.order.getType(), this.order.getId())) {
|
|
String msg = MessageFormat.format("The Order {0} already exists!", this.order.getLocator());
|
|
throw new StrolchException(msg);
|
|
}
|
|
|
|
orderMap.add(tx(), this.order);
|
|
}
|
|
|
|
@Override
|
|
public void undo() {
|
|
if (this.order != null && tx().isRollingBack()) {
|
|
OrderMap orderMap = tx().getOrderMap();
|
|
if (orderMap.hasElement(tx(), this.order.getType(), this.order.getId()))
|
|
orderMap.remove(tx(), this.order);
|
|
}
|
|
}
|
|
}
|
|
</pre>
|
|
|
|
|
|
<!-- content here -->
|
|
|
|
</div>
|
|
<!-- /.content -->
|
|
|
|
<div id="footer">
|
|
<div class="container">
|
|
<p class="text-muted">© Strolch / <a href="mailto:eitch@eitchnet.ch">Robert von Burg</a> / Hosting by
|
|
<a href="http://www.eitchnet.ch">eitchnet.ch</a></p>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
<!-- /.container -->
|
|
|
|
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
|
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
|
|
<!-- Include all compiled plugins (below), or include individual xsd as needed -->
|
|
<script src="js/bootstrap.min.js"></script>
|
|
|
|
<!-- Piwik -->
|
|
<script type="text/javascript">
|
|
var _paq = _paq || [];
|
|
_paq.push(['trackPageView']);
|
|
_paq.push(['enableLinkTracking']);
|
|
(function () {
|
|
var u = (("https:" == document.location.protocol) ? "https" : "http") + "://piwik.eitchnet.ch/";
|
|
_paq.push(['setTrackerUrl', u + 'piwik.php']);
|
|
_paq.push(['setSiteId', 2]);
|
|
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
|
|
g.type = 'text/javascript';
|
|
g.defer = true;
|
|
g.async = true;
|
|
g.src = u + 'piwik.js';
|
|
s.parentNode.insertBefore(g, s);
|
|
})();
|
|
</script>
|
|
<noscript><p><img src="http://piwik.eitchnet.ch/piwik.php?idsite=2" style="border:0;" alt="" /></p></noscript>
|
|
<!-- End Piwik Code -->
|
|
|
|
</body>
|
|
</html>
|