Is there a way to find Liferay users by FullName? I see there are methods to get a user by screen name and email address, but did not come across anything to get users based on full name. Is it possible?
To get a list of users by their name you'll need to use one of UserLocalServiceUtil's or UserServiceUtil's search method. Like the one below for Liferay Portal 6.1:
public List<User> search(
long companyId, String firstName, String middleName,
String lastName, String screenName, String emailAddress, int status,
LinkedHashMap<String, Object> params, boolean andSearch, int start,
int end, OrderByComparator obc)
throws SystemException
You can pass null for any field, including obc, if you have no other requirement or desired ordering.
If you want to fetch users from database, then dynamic query API can be used to write your own query to fetch matched details.
You may refer this wiki page about writing dynamic queries.
If you like to fetch from search indexer, then faceted search may be used.
Related
I have domain object like this:
class Customer
{
string FirstName {get;set;}
string LastName {get;set;}
DateTime DateOfBirth {get;set;}
}
Product team told me: We have to get customer by ID. Customer has information like FirstName, LastName, DateOfBirth, Age and blank fields. Age and blank fields can be calculated.
There is no application, just API. Who consumes this API doesn't matter.
Q: If I follow Domain Driven Design how domain class Customer looks? Where I put fields like Age and list of blank fields (for every Customer)? How business logic class looks like?
I think you have an anaemic model going here. The age should be implemented completely in the Customer class. So that to access the value, you do, customer.age. The blankfields might be a concept that needs it's own entity/domain, because a "Customer" cannot have a "blankfield"; the language doesn't fit/make sense. If you need the "blank fields" to exist as part of a customer object though, consider using a value object inside the customer object as well.
You don't need the service doing all that you have it doing. The only reason the service might be involved in this is if there's no way you can have your entity doing the work because of an external dependency or possibly complexity.
So, your service constructs your database from your persisted data and that's the end of it's involvement. In fact, you should probably be using a repository for re-constituting your object (instead of a service).
I would like to ask if there is any Java API call in liferay which returns the web contents, which were uploaded by a specific user.
For example, I have one user who has upload some content and I want to show in a portlet this content, how can I do this via java?
If you are specifically talking about web-content which is displayed inside Web-content Display portlet, then you can use the JournalArticleService and JournalArticleLocalService API to fetch the content depending upon the User.
Prior to Liferay 6.0 Web-content was known as JournalArticle and hence the API name has not changed.
So for example:
You can use DynamicQuery API, as follows:
long userId = 10987L; // ofcourse you need to find this
DynamicQuery dynamicQuery = JournalArticleLocalServiceUtil.dynamicQuery();
dynamicQuery.add(RestrictionsFactoryUtil.eq("userId", userId));
int startOfList = 0;
int endOfList = 1000;
// if you want all the JournalArticle retrieved then use:
// int endOfList = QueryUtil.ALL_POS;
// this will retrieve the list of webcontents
List<JournalArticle> articles = (List<JournalArticle>) JournalArticleLocalServiceUtil.dynamicQuery(dynamicQuery, startOfList, endOfList);
The above code will retrieve all the JournalArticles so you would get all the versions of a single web-content since all these versions are stored in the same JournalArticle table. So for this you can add conditions to the dynamicQuery for the fields like version, id, resourcePrimKey, articleId, groupId, companyId etc.
Or if you have more complex needs than you can create a custom-sql-finder in liferay to fetch the desired data from any combination of Liferay DB tables.
If you are talking about contents as in Blogs, Wikis, Files, Webcontents etc then either use their respective *LocalServiceUtil or you can use AssetEntryLocalServiceUtil to fetch the assets for a particular User.
So with AssetEntryLocalServiceUtil also you can use the DynamicQuery API as shown above. The code may not be same but will be along the same lines.
You can know more about DynamicQuery API from this blog.
This is a long question so i am gonna go straight to the point. This is pseudo code for better illustration of the problem
DB Structure
User (UserID, Name, LastName)
Address(AddressID, UserID, Street, City, State, ZipCode) =>Many to One User relationship
Phone (PhoneID, UserID, Number, IsPrimary) =>Many to One User relationship
Domain Classes
class User:IEntity
{
public string Name {get;set;}
public string LastName {get;set;}
public ContactInfo{get;set;}
}
class Phone: IValueObject or IEntity? will see later.
{
public int id; // persistence ID, not domain ID
public string Number {get;set;}
}
class Address: IValueObject or IEntity? will see later.
{
public string Line1 {get;set;}
public string City {get;set;}
public string State {get;set;}
public string ZipCode {get;set;}
}
class ContactInfo: IValueObject or IEntity? will see later.
{
List<Address> Addresses {get;set;}
List<Phone> PhoneNumbers {get;set;}
}
So, so far we have a very basic representation of this domain and its models.
My question is the following. Let's say that i want to Update one of the addreses or fix the area code for one of the numbers because of misspelling wnen it was initially typed in.
If i follow Evan's bible about DDD, Value Objects should be immutable. Meaning, no changes to its properties or fields after it was created.
If that's the case, then i guess, none of my classes are a ValueObject, since i can't just recreate the whole ContactInfo class just because one portion of the string in the phone number is wrong. So, i guess that makes all my classes Entities?
Keep in mind that i have a "persistence id" for each of this classes since they are stored in a database.
Let's say that i decide to make Phone a value object, since it's easy to recreate in the constructor
public Phone(string newNumber)
so, it would be something like adding a method to User (agg root) AND contactinfo? (Demeter Law)
like...
User....
public void UpdatePrimaryPhoneNumber(string number)
{
this.ContactInfo.UpdatePrimaryPhoneNumber(number);
}
ContactInfo....
public void UpdatePrimaryPhoneNumber(string number)
{
var oldPhone = Phones.Where(p=>p.IsPrimary).Single();
var newPhone = new Phone(number, oldPhone.persistenceid???-> this is not part of the domain)
oldPhone = newPhone;
}
but i still have to deal with persistence id... grrrrr. what a headache.
Sometimes i feel when i read those blogs that most "ddd experts" that value objects are overused or i would say misused.
What would be the best solution to this scenario?
Thank you
If i follow Evan's bible about DDD, Value Objects should be immutable.
Meaning, no changes to its properties or fields after it was created.
If that's the case, then i guess, none of my classes are a
ValueObject, since i can't just recreate the whole ContactInfo class
just because one portion of the string in the phone number is wrong.
So, i guess that makes all my classes Entities?
While the VO itself may be immutable, a VO doesn't exist on its own - it is always part of an aggregate. Therefore, a VO can be immutable, but the object which references that VO doesn't have to be. What helped me understand VOs is to compare them to something like a primitive Int32 value. The value of each individual integer is immutable - a 5 is always a 5. But anywhere you have an Int32 you can set another value there.
For you domain, what that means is that you can have an immutable address VO, but a given use entity can reference any instance of an address VO. This is what will allow corrections and any other changes to be made. You don't change the individual fields on the address VO - you replace it with a whole new VO instance.
Next, "Persistence ids" shouldn't be expressed in anywhere in domain code. They exist solely to satisfy the needs of the relational databases and NoSQL databases don't require them at all.
The primary phone scenario should look more like this:
public void UpdatePrimaryPhoneNumber(string number)
{
var existingPrimaryNumber = this.Phones.FirstOrDefault(x => x.IsPrimary == true);
if (existingPrimaryNumber != null)
this.Phones.Remove(existingPrimaryNumber);
this.Phones.Add(new Phone(phoneNumber: number, isPrimary = true));
}
This method encapsulates the idea of updating an existing primary phone number. The fact that phone number VOs are immutable means that you have to remove an existing value and replace it with a new one. What usually happens on the database end, especially with ORMs like NHibernate, is it will issue a SQL delete and a subsequent insert to effectively replace all phone numbers. This is OK since the ID of the VOs doesn't matter.
An Entity has a rather unique and individual life-cycle. It has meaning when it stands alone.
The classic example of Order/OrderItem may help with this.
If an OrderItem becomes an Entity it would have a life-cycle of its own. However, this doesn't make too much sense since it is part of an Order. This always seems obvious when looking at an order but less so when looking at your own classes because there can be some references between classes. For instance, an OrderItem represents some Product that we are selling. A Product has a life-cycle of its own. We can have an independent list of Products. How we model the link between an OrderItem and the Product is probably another discussion but I would denormalize the Product data I require into the OrderItem and store the original Product.Id also.
So is the Address class an Entity or a Value Object? This is always an interesting one in that we have that favourite of answers: it depends.
It will be context-specific. But ask yourself whether you have (or need) an independent list of Addresss and then only have a need for the link to that Address in your User. If this is the case then it is an Entity. If, however, your Address makes sense only when it is part of your User then it is a Value Object.
The fact that a Value Object is immutable does not mean you need to replace more than just the specific Value Object. I don't know if I would have a ContactInfo class in your current design since it only wraps the two collections (Address/PhoneNumber) but I would keep it if there is more to it (probably is). So simply replace the relevant PhoneNumber. If you have something like primary/secondary then it is as simple as:
AR.ReplacePrimaryPhoneNumber(new PhoneNumber('...'))
If it is a list of arbitrary numbers then a Remove/Add would be appropriate.
Now for the persistence Id. You do not need one. When you have a primary/secondary scenario you know what your use case is and you can execute the relevant queries in your DB (to update the primary PhoneNumber, for instance). If you have an arbitrary list you may go for add all new numbers in my list and delete those numbers from the DB not in my list; else just delete all the numbers and add everything you have. If this seems like a lot of heavy movement: it is. Event sourcing would move a lot of this to in-memory processing and it is something I will be pushing for seriously going forward.
I hope this all makes sense. Getting away from focusing on the data side of things is rather difficult but necessary. Focus on the domain as though you have no database. When you find friction then do your utmost to not pull database thinking into your domain but try to think about ways you could keep your domain clean and still use your DB of choice.
I would create a class PhoneNumber which contains the String number of the current Phone class and use that as a Value object within your Phone class:
class Phone implements IEntity
{
public int id; // persistence ID, not domain ID
public PhoneNumber number {get;set;}
}
class PhoneNumber implements IValueObject
{
public String number {get;set;};
}
Later when your code evolves you will need (for example) phone number validation and you can put it in the PhoneNumber class. This class can then be reused over the whole application at different places.
The Address is in my opinion a Value object which you can treat like a whole. Although you could model Street, City, etc... which are normally entities, but this is probably over-modelling. No part of the address can change, the whole object is always replaced when changing after initial creation.
The User class is within this example with these boundaries an Aggregate root (and thus also an Entity).
The ContactInfo class is not a ValueObject (not immutable) and not an Entity (no real identity) but an Aggregate. It contains multiple classes which should be seen as a whole.
More info on http://martinfowler.com/bliki/DDD_Aggregate.html
Usually whenever a persistence id is there you should be thinking of an Entity.
If however you would want to add the persistence id's, I would start splitting like the Phone and PhoneNumber class. For example Address (Entity containing id) and AddressValue containing all the other fields (and logic about address values).
This should also solve the headache about managing the persistence identities, since you replace the whole value object and the persistence identity stays the same in case of the updatePrimaryPhoneNumber.
I have started developing portlets with Liferay and I would like to show one (or more) Web-content article(s) with a specified structure.
For example, suppose I've a structure "A" so how can I get the last web-content article which is created using this structure?
This article explains how to get articles with a tag but not with a structure.
Thank you
The Liferay API Docs (this is for 6.1, as I don't know what version you're using) are your friend as is the Liferay source code.
In short you'll want to use one of the following API methods:
JournalArticleLocalServiceUtil.getStructureArticles(long groupId, String structureId);
JournalArticleLocalServiceUtil.getStructureArticles(long groupId, String structureId, int start, int end, OrderByComparator obc)
These rely on knowing the ID of the structure from which your content was generated, if you don't know what it is then you can use the following API method to get a list of all of them for your current Community:
JournalStructureLocalServiceUtil.getStructures(long groupId)
You can also use similar methods to find Journal Articles by the JournalTemplate that they use:
JournalTemplateLocalServiceUtil.getStructureTemplates(long groupId, String structureId);
JournalArticleLocalServiceUtil.getTemplateArticles(long groupId, String templateId);
JournalArticleLocalServiceUtil.getTemplateArticles(long groupId, String templateId, int start, int end, OrderByComparator obc)
Comment back if you have any questions, or if this answers your question please hit the "Accept answer" button tick! Thanks!
I'm developing a BlackBerry 5.0 app.
I have an entity to be displayed on screen in a grid format.
Entity: Employee
Fields: EmpId(int), FirstName(string), LastName(string), Hobby(string)
Once I display the list of entites (which I know how to do), I also need to provide an option for the user to be able to search for an employee (similar to the contacts list). However, the search should be a free-text search and on any field.
E.g. if I have 3 employees
1|Ian|Botham|Cricket
2|Ravi|Shastri|Cricket
3|Ravi|Bopara|Football
and if the user types Ravi, it should show up emp 2 & 3. If he types Cricket, it should show up 1&2 and so forth.
I have tried using KeywordFilterField. However, I'm able to search only on one field. How can I extend this to search for more fields? Or is there a different way to do this? Are there any out-of-the-box controls available for this kind of functionality?
Thanks in advance
Say you have a class for your entity
class Entity
{
int empId;
String firstName;
String lastName;
String hobby ;
public String getSearchableString()
{
return firstName+lastName+hobby;
}
}
Every time you do a search , check to compare entityObject.getSearchableString().
by doing it this way, everytime there is a match in either firstName,lastName or hobby, the search will pick up this object.