How to write data structure Graph using Java model class - java

A product can have distinctive sorts of attributes, and they can change with regard to items, such as a car can have taking after qualities: {cost, colour, price} and chair can have : {cost, width, profundity, stature}. Product attributes are organized in a progressive bunch structure, where a product attribute group can contain one or a few product attributes and/or attributes. Group can contain one or a more Product Attributes and/or Product Attribute Groups.
Write the code required to handle the over portrayed items and a print work that prints the information in such a way that it is evident which properties have a place to which item and which quality gather in case any.

Related

Java large list of object based on different conditions and business rules

Java : I have a large list of products object from MySQL DB. Now this has to be sorted and listed based on various rules.
For example:
The list has to be sorted based on the following conditions
Location Id of user and product has to be matched and this should the top priority in the listing. (Changes depending on the user location)
Featured product has to be take the next priority.
Any new preference in future should be accommodated as next in the list
Currently this is achieved by monolithic way by fetching objects from DB based on different conditions nd appending the list, but maintaining the redundancy (same products fall under multiple categories has to be managed) and when the list grow relatively large this will not be scalable.
I'm looking for an algorithmic way or any best approach
To keep it short I want to list products like how Amazon or any shopping platform lists based on different business rules depending on the user.

Redistribute/transform qty from one form to other

We are a distribution company where we buy products from supplier /distributor where the products are packed in boxes with a specific pack type and has other attributes associated to them like Branch, Country, Distributor name, etc.
Once these products are in our warehouse, we redistribute or repack them to as per our need and sell them. But if the seller specifies that some of the attributes of the product cannot be changed(We call them as locks), we retain them.
The below image shows issue data from the supplier and the Receive data(Repacked Data) which we do in our warehouse. If the pack type says 1X15 implies that the 1 box contains 15 products. The pack type always starts with 1X[Y] where Y is a variable.
Those attributes that cannot be changed are indicated in Locks column. Notice that the Qty that we get from the supplier and the qty we repack are same. Also, the Qty for the locked attribute products is the same.
We have a screen where the user keys in all the supplier data and the repacked data. But the user will not specify how the issue data is distributed or repacked.
My goal is to find a mapping data of the issue qty with the receive qty keeping the locks intact in a fast and efficient way.
Final Map data which we need to generate:

Hibernate Single Table Hierarchy

I'm still learning Hibernate (jpa annotation) with Spring, but is there a way I would be able to take a single table and output hierarchical structure? Eg, I have a table called 'books' with the columns: book_id, author, library, floor, section. Is there a way I could return it with library -> floor -> section -> all the books in that section.
I have a class called books that essentially provides the everything required for creating the table. I also created a controller that lets me retrieve all the books, and it outputs it onto the webpage in a JSON format. Is there a way I could do this so it'll be hierarchical.
My first thought would be to make classes called library, floor, and section. Library would hold the name of the library and a list of floors in that library, floor would hold hold the name of the floot and a list of sections in that floor, and section would hold the name of the section and a list of all the books in that section. It became very long and involved, and I couldn't even get it to work, so I was hoping there was a much simpler way of doing it.
Yes there is, you can map ORM hierarchy in one of 3 ways, single table, one per concrete class and one per relationship.
For single table you have to use a discriminator column in the table that differentiates data and tells what row is for which object.
Refer:
http://www.thejavageek.com/2014/05/14/jpa-single-table-inheritance-example/

Graph: What are pros and cons of modelling bidirectional edges as two separate edges?

I am currently working on an application where we are basically building up a graph between "items".
I think there are different kinds of links between those items, but I am struggeling right now to decide how to model those, especially bi-directional links.
1. Example:
ItemB --isChildOf---> ItemA
ItemA --isMasterOf--> ItemB
The same I think could be modelled like this:
Example 2
ItemA <---> ItemB
In my Database model I currently model my links like this:
* sourceID
* targetID
* relationType (e.g. isMasterOf, isChildOf, maybe more...)
Do I also need the direction as a field in my table? So far I don't have it as a separate field, because IMO the direction is implicitly defined by sourceID and targetID.
I am not sure in which situations I would need Example 1 and in which cases Example 2 is enough. I think Example 1 is like twitter, where UserA can follow userB but maybe not the other way round. Facebook on the other hand is always Example2 in my opinion.
Hope my question makes sense.
If you want a solution that is as generic as possible and will work in any simple graph modeling case (one-to-many/many-to-one, many-to-many), just model the links as (source_id, target_id) pairs in a separate link table. That way you can both
model bidirectional links by having edge pairs (a.id, b.id) and (b.id, a.id) and unidirectional links by having one of those depending on direction
differentiate edge direction by looking at which node is the source and which is the target - no need to have a separate direction field.
If you want to make an elegant solution for a particular case where there are actual constraints on what kind of graph you want to model, you will first need to decide
what kind of links you want to allow between objects: will they always be uni- or bidirectional, or do you need to differentiate both and
whether it will be a tree-like structure where one object can have just one parent but many children, or a more generic graph where one object may have several edges coming and going.
Regarding the first point, if you KNOW all the links will be of the same kind, you don't really need to differentiate edge (source_id, target_id) from edge (target_id, source_id) in the code, because both mean just an edge between two objects/nodes.
As far as the second point goes: if the graph you want to model resembles a tree or a forest (every object has 0 or 1 parents and 0 to n children, all the links are of the same type - uni- or bidirectional), you can just add something like parent_id to the objects themselves instead of modeling the connections on a separate link table.
Obviously, if you need to have other attributes to the edges (such as weight or some kind of type attribute other than direction), you'll have to add fields accordingly. In that case it would make most sense to model the edges on a separate table in any case, since the edges will not be just simple links between nodes but objects with actual properties.

java web application - model design for displaying list with a sql join

I am authoring a javabean and would like to know if it should include properties to set values obtained from a sql join?
Say I have database tables for Products and Orders. These are also my java bean names.
Now I want to display a list of all products but in addition to all the product properties, I want to have columns to display last purchase date and last purchased by
My db query to get the product list would need to do joins to gather the additional info. It does not seem correct to have setters and getters for `last purchase date and last purchased by' in Product.java model. I may want to have a 3rd column so constantly adding new columns to my bean doesn't make sense.
How do you go about this? I seem to encounter this when needing to display lists of models in my view.
Have a Map in Product. Upon firing the sql, store hte results of the join as key value pair in the Map. K=lastpurchasedby(columnName) and V= the value
So no need to add individual attributes. They all come in key-value pairs.

Categories