I have two colections in mongodb sring boot:
public class StampOperation {
#Id
private String id;
private int addedQuantity;
private int previousQuantity;
private int remainingQuantity;
#JsonFormat(pattern="dd/MM/yyyy")
private Date operationDate;
#DBRef
private TaxStamp taxStamp;
}
public class TaxStamp
{
#Id
private String id;
private String name;
private double value;
private EType stampType;
private int globalInitialQuantity;
private int minimumThreshold;
}
is there any wa to multiply addedQuantity from StampOperation by value from TaxStamp ?
If I understand your question correctly, you want to know, how to multiply two values from two different objects.
double result = stampOperation.addedQuantity * taxStamp.value;
To do this operation, you need to have the referenced instances of both classes at hand. Also, the attributes which are supposed to be used (addedQuantity and value) need to be accessible. This could be done by e.g. marking them as public.
I need to get some specific data from rest to my model.
Data I get from rest :
[{"id":595,"name":"TransXChange20210805113332","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-08-05T09:45:21.444949Z","edited":"2021-08-05T09:45:27.542152Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 08.08.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210805113332.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210805113332.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false},{"id":538,"name":"TransXChange20210513082611","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-05-13T06:28:50.233464Z","edited":"2021-07-28T08:52:06.695966Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 15.05.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210513082611.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210513082611.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false},{"id":544,"name":"TransXChange20210526143716","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-05-26T12:40:42.587492Z","edited":"2021-07-28T08:52:04.417450Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 01.06.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210526143716.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210526143716.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false}]
i got it in single line
my code in java :
RestTemplateBuilder builder = new RestTemplateBuilder();
String soc = builder.build().getForObject("https://otwartedane.erzeszow.pl/v1/datasets/16/resources/", String.class);
assert soc != null;
System.out.println(soc);
And the problem is I need to put them into model
My model:
public class Resource {
private long id;
private String name;
private long prosystem_file_id;
private int dataset;
private int creator;
private int editor;
private LocalDateTime created;
private LocalDateTime edited;
private boolean update_from_url;
private String description;
private String file;
private String link;
private String extension;
private String data_type;
private String file_name;
private String chart_info;
private String map_info;
private int score;
private boolean isPublic;
private String licence;
private String connection_dict;
private String selection_string;
private String db_type;
private String type;
private int dbview_cache_minutes;
private String preview_base64;
private String gpkg_display_info;
private boolean archive_to_csv;
All getters and setters are generated.
How can I put them into model for example List?
The problem is I get data like [{resource},{resource},{resource}].
As it states from the RestTemplate documentation
you can call to the getForObject method passing as an argument the desired deserialization class.
Something like:
RestTemplateBuilder builder = new RestTemplateBuilder();
Resource soc = builder.build().getForObject("https://otwartedane.erzeszow.pl/v1/datasets/16/resources/", Resource.class);
assert soc != null;
System.out.println(soc);
I have found answer to my question.
I need to assign my call to array of model.
Resource[] soc =builder.build().getForObject( URL , Resource[].class );
I would like to map the following two classes using Orika:
public class PagedList<T> extends ArrayList<T> {
private static final long serialVersionUID = 1L;
#JsonProperty("total")
private Integer total;
#JsonProperty("offset")
private Integer offset;
#JsonProperty("count")
private Integer count = PagingService.PAGE_SIZE_DEFAULT_VALUE;
...
and
public class ListUsersResult {
private List<UserDto> users;
private Integer total;
private Integer offset;
private Integer pageSize;
...
}
so that the entire PagedList ends up in users property on ListUsersResult along with total, object and pageSize. I only see ways of mapping fields/properties from source to destination. Ideally I would like not to use customize method.
n00b Java/hibernate question. I want to persist my class DataStoreProvider using Hibernate defined like so -
class DataStoreProvider{
#ElementCollection
#MapKeyClass(value = NativeType.class)
protected ConcurrentHashMap<NativeType, ODBCType> nativeToODBC;
protected String name;
//getters and setters
}
Referenced user defined classes are defined like so -
final public class NativeType {
private String name;
private int maxPrecision;
private byte maxScale;
public NativeType(String name, int maxPrecision, byte maxScale){
this.name = name;
this.maxPrecision = maxPrecision;
this.maxScale = maxScale;
}
// getters and setters
}
public enum ODBCType {
SQL_CHAR(1000000, 0),
SQL_VARCHAR(1000000, 0),
SQL_LONGVARCHAR(1000000, 0),
SQL_WCHAR(1000000, 0),
;
private final int maxPrecision;
private final int maxScale;
private ODBCType(int maxPrecision, int maxScale){
this.maxPrecision = maxPrecision;
this.maxScale = maxScale;
}
// getters and setters
}
NativeType technically only has an identity in the context of a DataStoreProvider. So I could include provider as an attribute of NativeType if needed, to make this clearer. And of course, the relationship NativeType-DataStoreProvider is also Many-to-One.
How do I do this so that I have one table for DataStoreProvider, and another where DataStoreProvider's Id is listed against - NativeType columns, and ODBCType enum value?
These are the Database Tables -
DataStoreProvider
===============
1|d1
2|d2
nativeToODBC
===============
1|#NativeType(VARCHAR2,4000,0)|ODBCType.SQL_VARCHAR
1|#NativeType(CHAR,4000,0)|ODBCType.SQL_CHAR
Thanks in advance!
I am using the GAE Datastore for the application
I am in despair ... I hit a mental block over here and I just cant think of anything to solve this anymore. I have a class Teacher.class (and all of its CASCADES) that wont store in its Namespace ... all other classes (not shown here, but very similar) work like a charm, the Teacher.class reads and writes and all perfectly ... except if WONT go to its Namespace, it always ends up in the Empty Namespace.
I am going to post the class, along with the data interface layer.
#Entity
public class Teacher implements Serializable
{
private static final long serialVersionUID = 5426530769458891752L;
#Id
private Key key;
private long KID;
private long school;
private String FName;
private String LName;
private String Email;
private String SchoolName;
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY)
private Transcript Transcript; // Contains further #OneToMany Relations and Constructors
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY)
private TeacherInfo teacherInf; // Contains Only Primitive Type Objects(not important)
private Boolean ActiveUser = false;
private List<Key> WorkshopsAttended;
private List<Key> WorkshopsRegistered;
public Teacher()//Constructor
{
if(this.KID == 00)
{
this.KID = TeacherUtils.genKID();//Returns a sequence and date and location based long
this.key = KeyFactory.createKey(Teacher.class.getSimpleName(), this.KID);
this.Transcript = new Transcript();
this.teacherInf = new TeacherInfo();
}
if(this.WorkshopsAttended == null)
{
this.WorkshopsAttended = new ArrayList<Key>();
}
if(this.WorkshopsRegistered == null)
{
this.WorkshopsRegistered = new ArrayList<Key>();
}
} //End of Constructor
//Getters and Setters
}
Here is the Transcript Class
#Entity
public class Transcript implements Serializable
{
private static final long serialVersionUID = -6677626465437896027L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key ID;
#OneToOne(cascade = CascadeType.ALL, mappedBy="Transcript",fetch=FetchType.LAZY)
private Teacher teacher;
public Transcript()//Constructor
{
if(this.C1== null)
{this.C1= new Course1();}
if (this.C2== null)
{this.C2= new Course2();}
if (this.C3== null)
{this.C3= new Course3();}
}//End of Constructor
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
private Course1 C1; // Contains only primitive feilds
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
private Course2 C2; // Contains only primitive feilds
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
private Course3 C3;
// Getters and Setters
Now Lastly the Data Interface Layer (This is a very long long file and I cant put it all in here, so I am just gonna paste the bits that have to do with persisting Teacher entities)
public class TeacherUtils
{
private static final String ActiveNamespace = SystemSettings.TeacherActive;
private static final String DeletedNamespace = SystemSettings.TeacherDeleted;
private static final boolean NSFlag = (SystemSettings.UseNameSpace & SystemSettings.TeacherNameSpace);
public synchronized static void SaveTeacher(Teacher teacher)
{
EntityManager em = getActiveEM();
em.persist(teacher);
em.flush();
closeEM(em);
}// End of SaveTeacher
public synchronized static void UpdateTeacher(Teacher teacher)
{
EntityManager em = getActiveEM();
em.merge(teacher);
em.flush();
closeEM(em);
}// End of Update Teacher
private synchronized static EntityManager getActiveEM()
{
EntityManager em;
try
{
if( (!NamespaceManager.get().equals(ActiveNamespace)) && TeacherUtils.NSFlag)
{
setNamespace();
}
}
finally
{
em = EMF.get().createEntityManager();
em.getTransaction().begin();
}
return em;
} // End of getAciveEM();
private synchronized static void closeEM(EntityManager em)
{
em.getTransaction().commit();
em.close();
NamespaceManager.set("");
while(!NamespaceManager.get().equals(""))
{}
return;
}// End of CloseEM(em)
private synchronized static void setNamespace()
{
if(TeacherUtils.NSFlag)
{
NamespaceManager.set(TeacherUtils.ActiveNamespace);
while(!NamespaceManager.get().equals(TeacherUtils.ActiveNamespace))
{}
}
}// End of setNamespace
A typical example in the Business Logic Layer would be
Teacher teacher = new Teacher();
teacher.setFName("John");
teacher.setLName("Smith");
teacher.setEmail("xyz#xyz.com");
TeacherUtils.SaveTeacher(teacher);
I would like to thank everyone who tried looking into this question ! Turns out there was something wrong with the Teacher.class constructor logic that caused a change in the Namespace, without reverting it to its previous state (the ID generator function). Now that I took care of it all works great !! I think it's just one of those days !!
Again thanks alot and I will be leaving this code as a template incase someone needs it.