dozer mapping list field - list

I've the following situation:
class A {
private String name;
private List<R> rs = new ArrayList<R>();
//get, set
}
class R {
private String content;
//get, set
}
class AH {
private String name;
private List<RH> rs = new ArrayList<RH>();
//get, set
}
class RH {
private String content;
//get, set
}
How to configure dozer to map no only name field (it's mapped correctly) from A to AH but also map automatically collection of R to collection of RH?

Dozer handles this automatically. My fault, there were no getters and setters.

Related

Update User defined object of the dynamoDb item using UpdateItemRequest

We have a DynamoDB table Test which has an attribute Template. Bellow are the class definitions. I would like to update the Template attribute or some of its attribute based on certain condition. I tried doing the same using UpdateItemRequest but unable to find a way to update the template attribute since everything is converted to either string, number or bytes.
Code for reference.
#DynamoDbBean
public class Test implements NoSQLEntity {
private String name;
private Template template;
#DynamoDbAttribute("name")
public String getName() {
return name;
}
#DynamoDbAttribute("template")
public Template getTemplate() {
return template;
}
}
#DynamoDbBean
public class Template {
private String pk;
private String name;
private List<String> demo;
#DynamoDbAttribute("pk")
public String getPk() {
return this.pk;
}
#DynamoDbAttribute("name")
public String getName() {
return name;
}
#DynamoDbAttribute("demo")
public List<String> getdemo() {
return demo;
}
}
Sample update code:
UpdateItemRequest request = UpdateItemRequest.builder()
.tableName("Test")
.key(itemKey)
.updateExpression("SET tmpt = :tmt")
.expressionAttributeValues(expressionValues)
.build();
Here I am unable to build the :tmt using the AttributeValue. Can someone please guide me?
Here is a Readme from the Eng team that you may find useful:
https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced
DynamoDb Enhanced provides DefaultAttributeConverterProvider that will identify a converter that can convert the specific Java type into the AttrbuteValue type that can used to update the item.
Smaple Code:
AttributeValue value = DefaultAttributeConverterProvider.create()
.converterFor(EnhancedType.documentOf(Template.class,
TableSchema.fromBean(Template.class)))
.transformFrom(template);
Can then put this value in the expressionValues map.
Map<String, AttributeValue> expressionValues = new HashMap<>();
expressionValues.put(":tmt", value);
UpdateItemRequest request = UpdateItemRequest.builder()
.tableName("Test")
.key(itemKey)
.updateExpression("SET tmpt = :tmt")
.expressionAttributeValues(expressionValues)
.build();

Unity3D - Integer List values don't get initialized in Constructor of Class

In my Unity3D Project I made a simple 2D Game and implemented a Data Manager to save and load Data. I created a List but get a NullReferenceException whenever I try to refer to it. I have a DataManager Class (saves & loads Data) and a UserData Class (stores the fields that are needed to be saved or loaded). In the User Data Class I declare in initialize a List of integer type. The List contains the unlocked Levels and it is initialized in the Constructor of the User Data Class.
This is the DataManager Class:
public static class DataManager
{
public static List<int> GetUnlockedLevels()
{
UserData userData = Load();
return userData.unlockedLevels; // This method returns nothing, not even null!
}
private static void Save(UserData data)
{
string path = GetDataFilePath();
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fileStream = File.Open(path, FileMode.OpenOrCreate))
{
binaryFormatter.Serialize(fileStream, data);
}
}
private static UserData Load()
{
string path = GetDataFilePath();
if (!File.Exists(path))
{
UserData userData = new UserData();
Save(userData);
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fileStream = File.Open(path, FileMode.Open))
{
return (UserData)binaryFormatter.Deserialize(fileStream);
}
}
And here comes the UserData Class:
[Serializable]
public class UserData
{
public int score;
public List<int> unlockedLevels;
public UserData()
{
score = 50;
unlockedLevels = new List<int>();
unlockedLevels.Add(1); //unlocked by default
unlockedLevels.Add(2); //unlocked by default
unlockedLevels.Add(3); //unlocked by default
}
}
The problem is: the first method of the DataManager "GetUnlockedLevels()" returns nothing.
The weird part: I have the exact same Data Manager in another project where it works properly. In that other project, the GetUnlockedLevels-method returns "System.Collections.Generic.List´1[System.Int32]" when I return it via "Debug.Log". But in the new project, the method returns literally nothing (not even null; the exception comes at a later point)
I am sure that I didn't make a copy-paste-mistake. What could be the root for this error?
What IDE are you using? I ask because the answer to your question is that you have a typo in your UserData constructor. Normally you would be alerted to this as soon as you built within your IDE as it would fail to compile.
[Serializable]
public class UserData
{
public int score;
public List<int> unlockedlevels; // This line
public UserData()
{
score = 50;
unlockedLevels = new List<int>(); // And this line
unlockedLevels.Add(1); // And this line
unlockedLevels.Add(2); // And this line
unlockedLevels.Add(3); // And this line
}
}
unlockedLevels
vs
unlockedlevels
Solution: the file was created but was empty. I manually deleted the saved file and from then it worked properly.

Is DynamoDB sorting impossible?

I connected DynamoDB with Spring-boot.
Normal findAll() is work.
Pageable is work too.
But when I insert sort value, it's not a work.
And I tried to use findAllByOrderByBn() method.
This is my table information.
Partition key(hash key) is id.
Sort key(range key) is bn.
How can I sort?
#Data
#DynamoDBTable(tableName = "tableName")
public class EntityName implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#DynamoDBHashKey
#DynamoDBAutoGeneratedKey
private String id;
#DynamoDBAttribute private String bn;
#DynamoDBAttribute private String fr; // from
#DynamoDBAttribute private String to; // to
}
#EnableScan
#EnableScanCount
//public interface RepositoryName extends CrudRepository<EntityName, String> {
public interface EthereumTransactionRepository extends PagingAndSortingRepository<EntityName, String> {
/* Find */
Page<EntityName> findAll(Pageable pageable);
Page<EntityName> findByFrAndToBeforeOrderByBnDesc(String fr, String to, Pageable pageable);
}
If you need more code, reply me.
Firstly, if you are trying to sort based on range key, set the sort key. Annotation for the sort key is #DynamoDBRangeKey. So change the class to below
public class EntityName implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#DynamoDBHashKey
#DynamoDBAutoGeneratedKey
private String id;
#DynamoDBRangeKey private String bn;
#DynamoDBAttribute private String fr; // from
#DynamoDBAttribute private String to; // to
And also if you want to sort in ascending or descending, you can refer to following documentation : https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBQueryExpression.html#withScanIndexForward-boolean-

Spring REST 3 and JacksonMapper for JSON

We are currently using spring web services that support application/xml and application/json that works fine. A new service requires returning of an XML string. The string must be kept as XML but I find that application/json is also serializing the contents of the string. Is there any way to prevent this ?
Edits:
//dto:
#XmlRootElement(name="mydata")
public class MyData {
private String someData;
#XmlElement(required = true)
private String content;
#XmlElement
public String getSomeData(){
return someData;
}
public String getContent() {
return content;
}
//setter and other fields...
}
From the above, the content field is an XML string in my class. I would like the content field to be returned but not serialized. Is this possible ?
If this string is just serialized object (just like JSON with #ResponseBody), than everything you need is jackson-jaxrs lib and marking return bean with javax.xml.bind.annotation.* annotations:
//in controller:
#RequestMapping
public #ResponseBody MyData getMyData(){
MyData md = new MyData();
//...
return md;
}
//dto:
#XmlRootElement(name="mydata")
public class MyData {
private String someData;
#XmlElement
public String getSomeData(){
return someData;
}
//setter and other fields...
}
If you want to return XML, then add this annotation to the service:
#RequestMapping(method = RequestMethod.POST, value = "xyz", headers = "Accept=application/xml")

List<string> won't add XML-generated values when their valid

My program errors out with a Null Reference Exception when it attempts to add to a List
Code for the for loop
for (int i = 0; i < UserCourses.Length; i++)
{
CurrentUser.Course_ID.Add(UserCourses[i]);
}
Code for CurrentUser (which is a new of type User)
public class User
{
public int coursenum;
public string Username;
public string Password;
public string FirstName;
public string LastName;
public string Email_Address;
public string User_Type;
public List<string> Course_ID;
public List<Course> Course;
}
I had it display the UserCourses[i] and it displayed successfully with the correct information, what am I doing wrong here?
You are not initializing the Course_ID property to contain a reference to a new List<string>. So you are calling Add on a null reference.
(Also, you might consider using the AddRange method, which will add the whole array/list you are attempting to add with one line of code. This will eliminate the need to write your own loop.)