Bind two input elements to a single model attribute - java

I have a simple form with two radios and two dropdowns populated with values from the same ENUM class for each radio respectively. At the same time I have to use a model which has only one attribute for the dropdown.
The problem is this model is used by a service, which I cannot change and it expects only this concrete model attribute. I need to decouple and temporarily save the values of the two dropdowns separately in the model. And then based on the selection from the radios, get the temporary value and assign it to the model attribute which then is used by this service.
Do you have a good idea, recommendation how best to achieve something like this?
Currently I have only a getter + setter for this attribute. But if I introduce another two temporary attributes with their gettrers and setters, how do I then assign their values to the 'global' attribute I want to send further?
public class TimeoutSetting implements Serializable {
public TimeoutEnum timeoutEnum;
public void setTimeoutEnum(TimeoutEnum timeoutEnum) {
this.timeoutEnum = timeoutEnum;
}
public TimeoutEnum getTimeoutEnum() {
return timeoutEnum;
}
}
So the timeoutEnum needs to be sort of mapped to two additional attributes. Let's say firstTimeout and secondTimeout, where I store the values from the first and second dropdowns. But then I send to the service only the value in the timeoutEnum. I need something similar, such that in the UI the two dropdowns are decoupled from one another, i.e. if I change the value of firstDropdown, the second one stays unchanged and so on (remember they are part of radio buttons, so only ONE is active at a time).

Related

Vaadin - adding columns to grid from map

I want to show data in vaadin's grid, but I want to create columns dynamically for each value from customAttributes list. My data model more or less look like this
Item
name: String
localization: Localization
visible: Boolean
customAttributes: List<CustomAttribute>
CustomAttribute
name: String
value: String
Each Item has the same set of attribute types, only the values are different. User can manually defined new attribute types.
How can I create this grid?
Currently I do it in this way:
grid.setColumns("name", "visible");
grid.addColumn(v -> v.getLocalization().getName()).setHeader("Localization");
But I have no idea for dynamic creating columns for each custom attribute.
Like it was already written in the comments, you can loop over the attribute names assuming your have a list (or set) of them. One small gotcha is that you need to do that in a way that ensures values inside the callback are effectively final.
attributeNames.forEach(name -> {
grid.addColumn(item -> item.getCustomAttributes().get(name))
.setHeader(name);
});
If you don't directly know the attribute names but you're loading all items into memory instead of lazy loading them, then you can find the unique names by iterating over all items:
items.stream().flatMap(item -> item.getCustomAttributes().keySet().stream())
.distinct().forEach(<same loop as in the previous example>);

How can I access variables from another class JAVA

So i am developing a program to simulate a collection system and the main system takes in items but there are different types of items such as those that require an id check etc... I am using a polymorphic method to access these different classes however is it possible for me to change a variable from the main class based off what occurs in the method that accesses the other class.
EX:
itemCollection firstCollect = new itemCollection();
Item test = new AlcoholItem(5.94, false, 3.76. 0.06) // takes in weight, bulk, price, and sinTaxRate.
Lets say I have to keep an expected weight of the collection for all non bulk items and have a variable in the itemCollection class called bulk, how can I use the fact that this item isn't bulk. Can I from within the AlcoholItem class method which is Polymorphic can I change itemCollection's bulk variable from the AlcoholItem class?
How can I access variables from another class JAVA ?
Generally you want to avoid that and favor methods for that : getters to retrieve values and setters to set values from objects of other classes.
Can I from within the AlcoholItem class method which is Polymorphic
can I change itemCollection's bulk variable from the AlcoholItem
class?
Adding a bulk item doesn't mean that all items are bulk. You should give a clear semantic about the bulk field in ItemCollection.
About updating the weight for no bulk items in the collection, I would do things in the other way since the collection of items depends on the items : when you add an item in the collection, check whether the item is not bulk, if it is not : update the weight of the collection according to.
Generally you want to avoid bidirectional coupling between classes/objects as much as possible.
You should also define a boolean isBulk() method in the item class/interface.
In ItemCollection class the add() could look like:
public void add(Item item){
if (!item.isBulk()){
weight += item.getWeight();
}
else{
bulk = item.isBulk(); // as discussed check the relevance of this flag.
}
// ...
}

is it possible to create a hash map where each value is a method that acts differently on an object

Problem
I want to know if this is possible if I could create a State machine that would contain all the methods and the Values of MethodById would be stated in the machine.
P.S. this is my first question ever on here. If I do it wrong I'm sorry but that is why.
Description (TL;DR)
I'm trying to cross reference data about Sales representatives. Each rep has territories specified by zip-codes.
One dataset has the reps, their territories and their company.
Another data set has their names, phone number and email.
I made a Sales-rep class that takes from the first data-set and needs to be updated with the second data-set.
I also need the Sales-reps to be put in a look-up table (I used a hashmap for this) of <key: zip code, value: Sales-rep object>.
What I want is for each Sales-rep object to having an ID that is standard across all my datasets. I can't use the data I'm provided with because it comes from many different sources and its impossible to standardize any data field.
Names, for example, are listed so many different ways it would be impossible to reconcile them and use that as an ID.
If I can get an ID like this (something like an SSN but less sensitive) then I want to try what my question is about.
I want to iterate through all the elements in my <key: zip code, value: Sales-rep object> hashmap, we will call it RepsByZipCode. When I iterate through each Salesrep object I want to get an ID that I can use in a different hashmap called MethodById <key: ID, value: a method run on the Object with this ID>.
I want it to run a different method for each key on the Object with the matching key (AKA the ID). The point is to run a different method on each different object in linear time so that by the end of the for loop, each object in RepsByZipCode will have some method run on it that can update information (thus completing the cross-referencing).
This also makes the code very extendable because I can change the method for each key if I want to update things differently. Ex:
//SalesRep Object Constructor:
SalesRep(String name, String email, ..., String Id)
Map<String zipcode, Salesrep rep> RepsByZipCode = new HashMap<>{}
//code fills in the above with the first dataset
Map<String ID, ??? method> MethodById = new HashMap<>{}
//code fills in the above with the second dataset
for(String ZipKey:RepsByZipCode){
Salesrep Rep = RepsByZipCode.get(ZipKey);
Rep.getId = ID;
MethodById.get(ID);
//each time this runs, one entry in RepsByZipCode is updated with one
//method from MethodById.
//after this for loop, all of RepsByZipCode has been updated in linear time
You could put these methods into different classes that implement a common interface, and store an instance of each class in your map. If you're using at least Java 8 and your methods are simple enough, you could use lambdas to avoid some boilerplate.

Updating only some attributes of objects in cache

Let's say I have a class Item. Items have object attributes and collection of other objects attributes:
public class Item
{
//Object attributes
String name;
int id;
Color color;
//Collection of object attributes
List<Parts> parts;
Map<int,Owner> ownersById;
}
I have a fairly simple web application that allows crud operations on these items. This is split up into separate operations:
a page where you can update the simple object attributes (name, id...).
a page where you can edit the collection of parts.
a page where you can edit the map of owners.
Because the server load was getting too high, I implemented a cache in the application which holds the "most recently used item objects" with their simple attributes and their collection attributes.
Whenever an edit is made to the name of an item, I want to do the following do things:
Persist the change to the item's name. This is done by converting the item object to xml (without any collection attributes) and calling a web service named "updateItemData".
Update the current user's cache by updating the relevant item's nme inside the cache. This way the cache stays relevant without having to load the item again after persisting it.
To do this I created the following method:
public void updateItem(Item itemWithoutCollectionData)
{
WebServiceInvoker.updateItemService(itemWithoutCollectionData)
Item cachedItemWithCollectionData = cache.getItemById(itemWithoutCollectionData.getId());
cachedItemWithCollectionData.setName(itemWithoutCollectionData.getName());
cachedItemWithCollectionData.setColor(itemWithoutCollectionData.getColor());
}
This method is very annoying because I have to copy the attributes one by one, because I cannot know beforehand which ones the user just updated. Bugs arised because the objects changed in one place but not in this piece of code. I can also not just do the following: cachedItem = itemWithoutCollectionData; because this would make me lose the collection information which is not present in the itemWithoutCollectionData variable.
Is there way to either:
Perhaps by reflection, to iterate over all the non-collection attributes in a class and thus write the code in a way that it does not matter if future fields are added or removed in the Item class
Find a way so that, if my Item class gains a new attribute, a warning is shown in the class that deals with the caching to signal "hey, you need to update me too!")?
an alternative which might seem a bit overkill: wrap all the non-collection attributes in a class, for example ItemSimpleData and use that object instead of separate attributes. However, this doesn't work well with inheritance. How would you implement this method in the following structure?
classes:
public class BaseItem
{
String name;
int id;
}
public class ColoredItem
{
Color color;
}
There many things that can be done to enhance what you currently have but I am going to point out just two things that may help you with your problem.
Firstly, I am assuming that public void updateItem is a simplified version from your production code. So; make sure this method is thread safe, since it is a common source or problems when it comes to caching.
Secondly, you mentioned that
Perhaps by reflection, to iterate over all the non-collection
attributes in a class and thus write the code in a way that it does
not matter if future fields are added or removed in the Item class.
If I understand the problem correctly; then, you can easily achieve this using BeanUtils.copyProperties() here is an example:
http://www.mkyong.com/java/how-to-use-reflection-to-copy-properties-from-pojo-to-other-java-beans/
I hope it helps.
Cheers,

Bean class Vs Collection : which one should i prefer to hold data

I have a TestDTO class which holds the 2 input data from user,
next step is to fetch the several data from database, lets say i am fetching ten String type values from database which requires further to execute the business logic.
I wanted to know the best way to hold the data (in terms of saving memory space and performance)
Add 10 more fields in the existing TestDTO class and set database values at run time
Use java.util.collection (List/Map/..)
Create another DTO/Bean class for 10 String values
If you want modularity of your code 3rd point is better, but for simplicity you should use a HashMap, like:
HashMap map = new HashMap();
map.put("string1",value);
.....
and so on.
This post can be useful for you : https://forums.oracle.com/thread/1153857
If TestDTO and the new values fetched are coming from the same table in the database, then they should be in the same class. Else, the new values should ideally be in another DTO. I do not know the exact scenario that you have, so given these constraints, 2nd option goes out of the window. And options 1 and 3 will depend on your scenario. Always hold values from a single table in one object(preferably).

Categories