Get only head values from firebase database - java

I have a users database in firebase that contains user's first name and last name
So, i want to take only the head name (i.e) Stephen, Steve, Tony e.t.c.
And i want to show them as a list in a recycler view.
Is this possible?
This is my java object
public class Users {
String firstName, lastName;
public Users(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
and i have created my adapter that extends RecyclerView.Adapter<>
PS: The names are not always same as the first name.

Related

AWS Lambda Constructor Overloading Java POJOs

I created a Lambda based on this URL: https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html. There are three main classes:
package example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class HelloPojo implements RequestHandler<RequestClass, ResponseClass>{
public ResponseClass handleRequest(RequestClass request, Context context){
String greetingString = String.format("Hello %s, %s.", request.firstName, request.lastName);
return new ResponseClass(greetingString);
}
}
is the Lambda handler.
package example;
public class RequestClass {
String firstName;
String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public RequestClass(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public RequestClass() {
}
}
is the requestClass.
package example;
public class ResponseClass {
String greetings;
public String getGreetings() {
return greetings;
}
public void setGreetings(String greetings) {
this.greetings = greetings;
}
public ResponseClass(String greetings) {
this.greetings = greetings;
}
public ResponseClass() {
}
}
is the response class.
Input is something along the lines of:
{ "firstName": "John", "lastName": "Doe" }
That being said, I do have some questions regarding the RequestClass. Right now, it is dependent on there being a firstName and lastName provided as part of the input. However, let's say only a firstName is provided, and I want to have another constructor in the RequestClass with just a firstName parameter and initialize the lastName to a default lastName, something like
public RequestClass(String firstName) {
this.firstName = firstName;
this.lastName = "defaultLastName";
}
When I try doing something along these lines and access the variables in the handleRequest, I'm able to get the firstName but lastName is always null (guessing because I do not provide it as part of the input). Any reason as to why this happens and what I can do so that when accessing lastName in the handler class, I am able to get "defaultLastName" instead of null?
Please let me know if there are further clarifications I should add, I don't post to StackOverflow very often and want to make sure my question is appropriate!
you could set some default values in your default constructor.
public RequestClass() {
firstName = "defaultFirstName";
lastName = "defaultLastName;
}
And then call your parameterized constructor with this.
public RequestClass(String firstName) {
this();
this.firstName = firstName;
}
Readings for constructor overloading:
https://beginnersbook.com/2013/05/constructor-overloading/

How to return custom object in a MBean interface

I have create a custom JavaBean I want to return from my MBean method. The following is the custom JavaBean:
package org.text.jmx;
public class Person {
private firstName;
private lastName;
public Person(){
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
The MBean interface is the following:
package org.text.jmx;
public interface TestJmxMBean {
public Person getPerson();
public void setPerson(String firstName, String lastName);
}
The class the implements the MBean:
package org.text.jmx;
public class TestJmx implements TestJmxMBean {
private Person person = new Person();
public Person getPerson() {
return person;
}
public void setPerson(String firstName, String lastName) {
person.setFirstName(firstName);
person.setLastName(lastName);
}
}
I create a server application that registers the above MBean, which is successful. I create a client application which successfully connects to the server application via JMX, but when I call the testJmx.getPerson() method from the client application is receive an error that it can't return the Person object. What am I doing wrong? It works fine is I just define the return type as as String or String[] from the TestJmx.getPerson().
In order to expose a custom object as a JMX attribute, or the return value or an operation, it must be defined as an OpenType. The usual way of doing this is to define an MXBean. I answered a similar question which should give you an idea on how to proceed.

How to print contents of Java object without using getters

Actually i am implementing one program in which i am retrieving information from database and i am setting that info to One Object using setters and i just want to print that Object parameters without using system.out.println(object.getHeadCount());
I just want like if i am giving system.out.println(object); so using this code it should print data in Json format or any other readable format.
How to do it.Because my object is containing 30 fields so it is very hectic to write 30 getters to print data.
You have to override toString()
Normal way
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return this.firstName + " " + this.lastName;
}
}
Using Apache Commons
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return new ToStringBuilder(this)
.append("firstName", firstName)
.append("lastName", lastName)
.toString();
}
}
Using Google Guava
class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString(){
return MoreObjects.toStringHelper(this)
.add("firstName", firstName)
.add("lastName", lastName)
.toString();
}
}
You simply have to override the toString() method of the class, read about it in java doc for Object class. Most IDE support it's automatic generation for all the fields of your class or for a number of them. Just for example:
class User {
private String name;
private String surname;
User(String name, String surname)
{
this.name = name;
this.surname = surname;
}
#Override
public String toString()
{
return this.name+" "+this.surname;
}
}
You should override toString() method. if you are using eclipse then you can try right-click within the editor, you'll find it under Source -> Generate toString()
To make a toString method you can simply just add one like so.
public String toString(){
return "" + getValue();
}
The toString method is a part of
java.lang.Object
So it is implemented in every class.

Let user add custom fields to object "class"

I've built a Java application for, globally, mange computers database at my job. At first, I've been told that we needed a tab called 'Users', which would contain first name, last name and email. But now, the technician wants to add other fields such as address, phone, etc. He asked me if he could add himself these fields. The problem is, he's not a programmer. He wants to add these fields with a GUI. I have built the application with static fields, and here is my User Class.
public class User {
private String firstname;
private String lastname;
private String email;
private int id;
public User(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public User(int id, String firstname, String lastname, String email) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public User(String firstname, String lastname, String email) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public String toString() {
return firstname + " " + lastname;
}
}
Now, I wonder if there's a way to modify this class using the GUI or if I have to rebuild entirely the software and stop using object classes like that. There are also classes like "Software" and "Operating Systems" that have static fields but need to be modified if necessary.
I don't know what options exactly I have to get the job done:
Let the tech modify the database and do something like "for each column in database, add this column in the GUI". (Which would cause to rebuild the entire software.)
others?
Any reads/tutorials on that kind of issues?
Thanks.
If the requirement is that a normal user should be able to add additonal fields to existing objects, propably the best way would be to store the information in a map.
So instead of:
public class User {
private String firstname;
private String lastname;
private String email;
private int id;
}
you would have:
public class User {
private int id;
private Map<String,String> properties;
public User(String firstname, String lastname) {
properties = new HashMap<String,String>();
properties.put("firstname",firstname);
properties.put("lastname",lastname);
}
etc.

objectify, javascript and google endpoint

I am trying to figure out how integration between google endpoint, objectify and javscripts works. I made a real simple class named 'User'. All I am trying is to fetch a record from a Data Store and return this object back and use this in javascript.
However, the object does not seem to flow back properly. I do not see any details around this object using Chrome's developer tools... Any ideas? FYI, the record exists in the data store as I can see it while using the development console. I also can see the information as I've logged this info to the console as well. Thanks.
** JAVA CLASS **
package com.Backend;
import com.googlecode.objectify.annotation.*;
#Entity
public class User {
String firstName;
String lastName;
#Id String email;
public User(){
super();
}
public User(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
** GOOGLE ENDPOINT **
public class UserEndpoint {
private static final Logger LOG = Logger.getLogger(UserEndpoint.class.getName());
static {
ObjectifyService.register(User.class);
}
#ApiMethod(name = "getUser")
public User getUser() {
User u = ofy().load().type(User.class).id("johndoe#domain.com").now();
return u;
}
}
Finally found out what the issue is. It has to do with how the object was returned... It did not have any return value.

Categories

Resources