Ignoring "Inactive" Entries from Every Search Method - grails

I'm using a legacy database in a read only fashion. I have a user domain that includes a flag if the user is active or not. Is there a way when calling search methods like findAll to always ignore inactive users without removing them from the database or specifically in every search query?

You have two options. Either add a Hibernate Filter which is your most transparent option, or to add a named query like
static namedQueries = {
active {
ne 'inactive', true
}
}
which will allow you to do
User.active.findAllBy...()

Related

Realm Swift: Question about Query-based public database

I’ve seen all around the documentation that Query-based sync is deprecated, so I’m wondering how should I got about my situation:
In my app (using Realm Cloud), I have a list of User objects with some information about each user, like their username. Upon user login (using Firebase), I need to check the whole User database to see if their username is unique. If I make this common realm using Full Sync, then all the users would synchronize and cache the whole database for each change right? How can I prevent that, if I only want the users to get a list of other users’ information at a certain point, without caching or re-synchronizing anything?
I know it's a possible duplicate of this question, but things have probably changed in four years.
The new MongoDB Realm gives you access to server level functions. This feature would allow you to query the list of existing users (for example) for a specific user name and return true if found or false if not (there are other options as well).
Check out the Functions documentation and there are some examples of how to call it from macOS/iOS in the Call a function section
I don't know the use case or what your objects look like but an example function to calculate a sum would like something like this. This sums the first two elements in the array and returns their result;
your_realm_app.functions.sum([1, 2]) { sum, error in
if let err = error {
print(err.localizedDescription)
return
}
if case let .double(x) = result {
print(x)
}
}

Use dynamic finders with list of Object IDs

With the given class structure
class MyObject {
Status status;
}
class Status {
Integer id;
}
I want to use dynamic finders to query based on a list of Status ID values. What I want to be able to do is something like this
MyObject.findAllByStatusInList([1,2,3]);
This does not work though because my list needs to be Status objects. I know I can build a criteria to do this, but I just want to know if there is a way to accomplish this with Dynamic Finders?
You can still accomplish this using the dynamic finder.
def statuses = [1, 2, 3].collect { Status.load(it) }
MyObject.findAllByStatusInList(statuses)
load() will create a proxy for you that won't require retrieving the instance from the database as long as you don't access any properties other than id.
You can use the where clause:
MyObject.where {status.id in [1,2,3]}.find()
UPD
Dynamic finders don't support aliasing so Criteria (or DetachedCriteria) is the solution to be used.
In case if Status class is a domain entity you could retrieve a list of them (or load their proxies) from the database and then query MyObjects by the status list.
So I see no other appropriate solution but using Criteria in your case.
You can use this.
List<Integer > statuses = [1, 2, 3]
MyObject.findAllByStatusInList(statuses)

Grails searchable returns domain but relation is null

I have 3 domains: A, B, C.
A and C have many-to-many relationship through B.
A and C are searchable.
When I search and get list of A domain all the fields in A are accessible, however relation field is always 'null'. Why I can't access relational field? Why do I get 'null'? If I access object directly, I see a relation, but when searchable returns A domain, I get 'null' on a relation field.
P.S: I tried to make B searchable but it looks like searchable having issue with indexing it on top of that I don't see any point in indexing B since it exists for the sake of many-to-many relationship only.
Please I need help with it. I need to be able to get to C via A on a searchable return.
Thank you.
[Update]: I made a prototype project today (small DB) and made domain B searchable. Now I can access relational field. However in my real project, I don't want to make B searchable since database is big and indexing takes too long. Is there a way around it?
You can refrash all instance in result list or use reload:true property for seach method
searchableService.search(query,[reload:true])
You can find full list of options: http://grails.org/Searchable+Plugin+-+Methods+-+search
reload - If true, reloads the objects from the database, attaching them to a Hibernate session, otherwise the objects are reconstructed from the index. Default is false
Ok, I believe I solved my issue.
First checkout link to a similar question: Grails searchable plugin -- Please give GalmWing some credit.
My solution is following, I am implementing my own controller and following piece of code implements searchable service call:
if (params.q) {
try{
def searchResults = searchableService.search(params.q, params)
searchResults.results.each {
it.refresh()
}
return [carInstanceList:searchResults.results, carInstanceTotal:searchResults.total]
} catch (SearchEngineQueryParseException ex) {
return [parseException: true]
}
As you can see I have a loop where on each item of domain "A" I do refresh. Now refresh gets real record from DB with all the links. Now I return list into GSP and it extracts all of "C" domains associated with the "A" domain.
Now this way you might get performance penalty, but in my case searchable is actually not able to index "B" domain, it is working for a while and then crashes, so I don't have another choice, at least for now.

Specify specific fields to audit in grails audit logging plugin

I am using the http://grails.org/plugin/audit-logging plugin and am only interested in auditing a single field in a large domain class. I could specify a lengthy 'ignore' list, but ideally I want to specify the whitelist of fields instead, so that if new fields are added, it is not necessary to maintain the ignore list to avoid them getting automatically audited which could be a performance risk.
Is this possible? I didn't see mention of it in the docs for the plugin.
I think you could do this using the event handlers only instead of the standard audit logging. You would set auditing like this
static auditable = [handlersOnly: true]
You could create a white list
def whiteList = ['name','age',...]
Then create on* events to handle a save, delete or change event and iterate through the white list to look up that key in the old and new map:
def onChange = {oldMap, newMap ->
whiteList.each{propName->
if(oldMap[propName] != newMap[propName]) {
//
}
}
}

Where to store a Doctrine variable created in a component so that it's accessible anywhere?

Note I am referring to one request, and not several requests and sessions.
I have several components that require Doctrine user object, some are located in layout, others are located in templates. Sometimes I need that Doctrine user object in action. Currently I have added a function to sfUser class that loads that object from database, which means every time I call that function I make a call to db. I'd like to know where to store this object so that I can access it without having to query db every time I need it. Again, we're talking about a single request, not several requests or something that would require session.
Can I save it in sfContext somehow? Any other places so that it can be available everywhere?
You can store it in your model's Table class, because tables are always accessed as singletones.
class sfGuardUserTable extends PluginsfGuardUserTable
{
protected $specialUser = null;
public function getSpecialUser()
{
if (null === $this->specialUser)
{
$this->specialUser = $this->findOneById(1);
}
return $this->specialUser;
}
}
Now, you can use this in actions and components like this:
$u = sfGuardUserTable::getInstance()->getSpecialUser();
And you will always end up with one query.
you can configure Doctrine cache so that the result of this specific request is always cached. What if so good about it is that if you use, say, the APC backend, you will have it cached across requests. You also get query caching as a bonus (this is not result caching, read the link I provided carefully)!

Resources