strolch/li.strolch.website/www.strolch.li/documentation-searches.html

183 lines
8.2 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: Searches</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: Searches</h1>
<p class="lead page-description">This page discusses Strolch Searches.</p>
</div>
<div class="content">
<p>As is custom for every framework, querying, or searching, the model must be possible. Strolch searches are
implemented using the <code>StrolchSearch</code> class and one of its concrete implementations: <code>ResourceSearch</code>,
<code>OrderSearch</code>, <code>ActivitySearch</code>.</p>
<p>A Strolch element always has two identifiers: <code>Type</code> and <code>Id</code>. The type is important as
it classifies an element. So if a car and a house would be modelled in Strolch, then those would both be a
<code>Resource</code>, but one of type <code>Car</code> and the other of type <code>House</code>. Both would
have different parameters. Thus when searching for objects, the first thing to do is define the type of
object being searched.</p>
<p>The Strolch search API is very expressive and offers multiple ways to perform the same search. The search API
consists of three components: The search classes, the search expressions and the search predicates. The
concept was taken from the <a href="https://camel.apache.org" target="_blank">Apache Camel</a> project.</p>
<p>There are four main search classes:</p>
<ul>
<li><code>RootElementSearch</code> - search for any of <code>Resource</code>, <code>Order</code> or <code>Activity</code>
elements
</li>
<li><code>ResourceSearch</code> - search for <code>Resources</code></li>
<li><code>OrderSearch</code> - search for <code>Orders</code></li>
<li><code>ActivitySearch</code> - search for <code>Activities</code></li>
</ul>
<p>No search is useful without a <code>where</code> clause, which are called search expressions. When writing a
search, there are multiple ways to add such where clauses. Either</p>
<ul>
<li>override the <code>define()</code> method in your sub class and add the where clauses by calling the
<code>where()</code> method, or
</li>
<li>define special methods on the class e.g. <code>byColor()</code> which also calls the
<code>where()</code> method to add a search expression, or
</li>
<li>directly call the <code>where()</code> method after instantiating a search.</li>
</ul>
<p>When extending the class, then the search expressions are available as methods on the super class, otherwise
you can statically import them from
<a href="https://github.com/4treesCH/strolch/blob/develop/li.strolch.agent/src/main/java/li/strolch/search/ExpressionsSupport.java"
target="_blank">ExpressionsSupport</a>.</p>
<p>And of course a where clause needs operators, which are called search predicates. Just as search expressions
are available in sub classes, so are search predicates and can also be statically imported through
<a href="https://github.com/4treesCH/strolch/blob/develop/li.strolch.agent/src/main/java/li/strolch/search/PredicatesSupport.java"
target="_blank">PredicatesSupport</a>.</p>
<p>Examples of search expressions with search predicates follow:</p>
<pre>
ResourceSearch search = new ResourceSearch();
// predicate either as parameter, or chained
search.where(id().isEqualTo("myId"));
search.where(id(isEqualTo("myId")));
// negating
search.where(id(isEqualTo("myId")).not());
search.where(param("bagId", "paramId").isIn(Arrays.asList("red", "blue", "green")));
search.where(paramNull("bagId", "paramId")));
// boolean operations
search.where(id(isEqualTo("myId")) //
.or(name(isEqualTo("myName"))));
</pre>
<p>Note how the predicates can be chained to the search expression, or passed as a parameter to the
expression.</p>
<p>In addition to using predefined search search expressions, one can also just pass a lambda expression which
performs a custom filter:</p>
<pre>
personSearch.where(person -> person.getName().length() == 3);</pre>
<p>See the
<a href="https://github.com/4treesCH/strolch/blob/develop/li.strolch.agent/src/test/java/li/strolch/search/StrolchSearchTest.java"
target="_blank">StrolchSearchTest</a> for many ways in which you can implement tests.</p>
<p>Note that strolch searches requires privileges. Thus when you use a strolch search, add it to the role of the
user in <code>PrivilegeRoles.xml</code>:</p>
<pre>
&lt;Privilege name="li.strolch.search.StrolchSearch" policy="DefaultPrivilege"&gt;
&lt;Allow&gt;internal&lt;/Allow&gt; &lt;!-- internal used for when the search is done in an internal service --&gt;
&lt;Allow&gt;li.strolch.bookshop.search.BookSearch&lt;/Allow&gt;
&lt;/Privilege&gt;</pre>
<!-- content here -->
</div>
<!-- /.content -->
<div id="footer">
<div class="container">
<p class="text-muted">&copy; 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>