Grails 3 scaffold rendering dropdown list for domains with foreigh keys - grails-3.1

If I have the following domain using Grails 3
class Books {
String Title
String Description
Author author
}
The generated scaffold create a dropdown list for author, which is good however, the values on the list contains;
webapp.author : 1
webapp.author : 2
webapp.author : 3
How can I make it display a field in the author domain, like author name and last name and remove project name, domain name and the id?

You need to add this to you Author Domain
String toString() {
"${AuthorName}"
}
Change AuthorName to the actual variable you want displayed on the dropdown list.

Related

Flutter: Filter products by categories in the app level?

I am currently trying to develop an app in which products are categorically displayed in a separate screen or class with ListView.builder ().
I have to select / favor a category or sub-category so that I can see products in the list view. I see nothin in the screen if i don't select one or more categorys OR subcategorys. I can also favor different categories and in the list view I see the products that match the favorite product. If I have favored a subcategory, I should only see in the list view the products that see in this favored subcategory and not the superordinate categories.
If I prefer a higher category, all products of the subcategories should be shown in the list view.
But I'm worried about the performance. My database will run on Firebase. Is firestore more suitable for this? I also read that relational databases are more suitable for such things. However, I have never worked with it and I want the favorites not only to be saved on the smartphone. I would like the favorites to be called up when the user logs in to the app with another phone.
My question is more of a structural question. Is it better for the performance if there is a Category class in which all higher and lower categories each have a list with the products and which are then merged in the list view (if different categories are selected, of course)?
I mean, if I list all the products in a single list and filter them afterwards in the code (at the app level), it is totally unperforming, right?
Do I have to have a class category and a separate subcategory class? How would you go about it? I have total chaos in my brain
class Product:
import 'package:flutter/material.dart';
class Product {
final String id;
final String title;
final List<String> imagesPath;
final String description;
final Color categoryColor;
Product({
#required this.id,
#required this.title,
#required this.imagesPath,
#required this.description,
#required this.categoryColor,
});
}
class Category:
import 'package:flutter/material.dart';
import '../models/Product.dart';
class Category {
final String title;
final Color color;
bool isFavourite;
final List<Product> products;
Category({#required this.title, #required this.color, this.isFavourite = false, this.products});
}
List<Category> categories = [
Category(
title: 'Automobile',
color: Colors.lightBlue,
isFavourite: false,
),
];
I no longer know which would be more correct in terms of how I should go on.
Thanks :)
I suggest using Cloud Firestore as it provides query functionalities that Firebase Realtime Database does not.
You can check this similar answer I've posted here where I've demonstrated querying Firestore data and filtering displayed data based from the selected item.

Writing a Custom Controller extension to get related records and iterate over list/index and use with apex:repeat

I have 3 custom objects with a Master-Detail Relationship and Lookup Relationship.
CustomA__c (related CustomB__c) <-> CustomB__c <-> CustomC_c (related CustomB_cc)
I´ve built a Visualforce page with HTML table to replicate a PDF document and a Custom Controller Extension, so far so good.
But I´m just a beginner with apex coding.
The problem is that I need to replicate the document as it is, when there are no related records for CustomA__c or less then 5, it should still show the full table (the empty rows). Max. rows/related records on the document is 5, no second page needed.
Currently I´m trying to accomplisch that by using apex:variable and apex:repeat as I´ve seen some examples, but perhaps there is also another solution. For the Visualforce page I already wrote the code for the rows with data and another apeax:repeat for the empty rows.
But I´m really strugling with the controller, i know i need to iterate over the list, the code that i already wrote is also put together out of examples as i just don´t understand it yet good enough.
Any help would be appreciated! Thanks, Josip
public with sharing class CustomAController {
public CustomA__c customa{get; set;}
public CustomA__c ca{get; set;}
public CustomAController (ApexPages.StandardController controller) {
ca = (CustomA__c )controller.getRecord();
customa= [SELECT Id, Name FROM CustomA__c WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
}
public List<CustomB__c > getrelatedCustomB() {
List <CustomB__c > cbList = New List<CustomB__c >(5);
for(CustomA__c acc:[SELECT Id, Name, (SELECT Id, Name, ... , CustomCfield__r.Name FROM CustomBs__r ORDER BY Name LIMIT 5) FROM CustomA__c WHERE Id = :customa.Id]){
for(CustomB__c cb:acc.CustomBs__r)
cbList.add(cb);
}
return cbList;
}
}
You can dramatically simplify your code by writing a direct query on the child object instead of a parent-child SOQL query.
public List<CustomB__c > getrelatedCustomB() {
return [SELECT Id, Name, ... , CustomCfield__r.Name
FROM CustomB__c
WHERE CustomA__c = :customA.Id
ORDER BY Name
LIMIT 5];
}
There's no need to iterate in Apex here.

AngularJS Dynamic email validation element, When I fill out the email check element, It's reset

I would like to create a form with several fields: name, last name, ... and add one or several email. The user should enter the first mandatory email address. After he should have the possibility to click on "Add email" for adding a new email address. He could add 4 others emails.
The system should be verify if the format is correct and register the data in a DB.
Could you tell me which is the best practice for doing that?
Kind Regards,
In the Database:
You should have separate table for storing email address's with foreign key that will be pointing to the main table
Table structure will look like this
ID,Email,Foreign-Key ID
In the Service layer you should have model class that will take email as array parameter
email class
class Email {
public int id,
public string email
}
in the main class
class Main {
email:Email[]
}
in the front end
you should clone the input type whenever the add email button is clicked and push the new element to an array and send it to the service layer

getting Value of a field by its Name in apex salesforce

in my visualforce page i have some campaign object first user select an object then there is a multi picklist. in this picklist there is Label for all the fields user selects some fields then i have to show the value of these fields in the selected campaign object
for showing multiple picklist my apex function is
public List<SelectOption> getOptionalFields(){
Map <String, Schema.SObjectField> fieldMap= Campaign.sObjectType.getDescribe().fields.getMap();
List<SelectOption> fieldsName =new List<SelectOption>();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
fieldsName.add(new SelectOption(dfield.getName(),dfield.getLabel()));
}
but i have no idea how to show value for the the field
for exmple i have object instance like
Campaign c;
now i have to get value of any field whose Name is in string form.how to get corresponding value for that field.one solution is just write like
say
String fieldName;
and use multiple if
if(fieldName=='Name')
c.Name=
if(fieldName=='Id')
c.Id=
is there any other convenient method??please explain!!
You need to read about "dynamic apex". Every "concrete" sObject (like Account, Contact, custom objects) can be cast down to generic sObject (or you can use the methods directly).
Object o = c.get(fieldName);
String returnValue = String.valueOf(o);
There are some useful examples on dynamic get and set methods on Salesforce-dedicated site: https://salesforce.stackexchange.com/questions/8325/retrieving-value-using-dynamic-soql https://salesforce.stackexchange.com/questions/4193/update-a-records-using-generic-fields (second question is a bit more advanced)
You'll still need to somehow decide when to return it as String, when as number, when as date... Just experiment with it and either do some simple mapping or use describe methods to learn the actual field type...

Grails iterating through database tables

As I'm a bit new to Grails, I'm wondering how I can iterate through the current data i have saved in the database to check if the information already exists.
For instance, lets say I have a domain class for Books and I create an action that automatically adds more books but I want to check if the book.title already exists so I don't add it again.
Side note
I'm just using the default database (whatever is used when the project is set to production mode)
Edit
I'll post my domains so it is a bit easier to understand. Instead of book.title, I changed it to where book belongs to author. So I only want the author added once but able to add many books for it. The issue happens in an action i created in the controller.
Author Domain:
class Author {
static hasMany = [books:Book]
String authorName
String notes
String age
String toString() { authorName }
static constraints = {
authorName()
notes(maxSize:500)
age()
}
}
Book Domain:
class Book {
static belongsTo = Author
String toString() { bookNumber }
Author bookAuthor
String title
String numberOfPages
static constraints = {
bookAuthor()
title()
numberOfPages()
}
}
Book Controller (this is where I'm having issues):
class BookController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
//took out index, create, list, etc. to focus on the once that I'm concerned about
//this action will simple read a text file and add books and authors
def gather = {
def parseData = new parseClient() //parses text file line by line and puts it into a list
def dataHolder = parseData.information //dataHolder will hold data from text file
int linesOfData = dataHolder.size() //get size to iterate and add authors & books
linesOfData.times {
def _author = dataHolder.author[it] //get author - Author
def _age = dataHolder.age[it] //get age - Author
def _title = dataHolder.title[it] //get title - Book
def _pages = dataHolder.pages[it] //get pages - Book
def authorInstance //create new Author to add
authorInstance = new Author() //for some reason I have to create and save AuthorName (can't have other fields) before I can add my Book correctly
authorInstance.setAuthorName(_author)
authorInstance.save()
def bookInstance
bookInstance = new Book() //create a new Book to add
bookInstance.setBookAuthor(authorInstance)
bookInstance.setTitle(_title)
bookInstance.setNumberOfPages(_pages)
bookInstance.save() //has to have access to the authorInstance to add correctly which is why i was wondering how to access the database to grab it if it existed
authorInstance.setAge(_age) //add whatever data is left for Author
authorInstance.save() //save again because cant save it with this information before I add authorInstance to the Book
}
}
}
Text File Content:
//You'll notice that author _Scott Davis_ is in here twice.
//I don't want to add two instances of Scott Davis but need to access it to add the book
//the unique constraint makes the value come up as not null but can't be added
Scott Davis : Groovy Recipes
Bashar Abdul Jawad : Groovy and Grails Recipes
Fergal Dearle : Groovy for Domain-Specific Languages
Scott Davis : GIS for Web Developers: Adding 'Where' to Your Web Applications
So I'm basically looking for a way to add that information and haven't found a way that seems to work without running into random problems.
Hope this clears my question up a bit, as the original question was a bit broad
In this case, you can put a unique constraint on title and grails will do that for you.
You can iterate thru the data, but you probably don't want to load the db if you can avoid it. So you could write a custom query to select the number of books for the title, for example.
Edit: for your updates
You don't need to use setters in your controller. Grails adds setters/getters for you dynamically at runtime. If you want to put some logic in your setters, then you can define your own and use them in that case
Have you looked at the grails documentation? http://grails.org/doc/latest/
you have a static constraints block, but you haven't defined how you want each property to be constrained. for unique title it would be
title(unique:true)
if you want to get a list of author names, you can do
List names = Author.executeQuery('select authorName from Author')

Resources