A proper way to serialize/deserialize Xodus-dnq entity - serialization

For example - I've got this entity:
class XdUser(entity: Entity) : XdEntity(entity) {
var someName by xdStringProp()
var someNumber by xdIntProp()
}
What is the proper way to serialize/deserialize it from/to json?
I have to create data class which just duplicates my entity's fields and then propagate values to XdUser? Or there is other way?

Serializing XdUser to JSON you should be sure that serializer won't process XdUser#entity and other public links which can expose large amount of unnecessary data. Deserialization brings another problems because deserializator should be informed about how to instantiate a class from json using constructor XdUser(entity: Entity).
From my prospective better choice there is to have another level for rest API. It brings ability to control amount of exposed data, control permissions (if your have them) and the way of how entities will be updated.
Rest api level be implemented like this:
open class EntityVO<T: XdEntity>(xdId: String?) {
}
class UserVO(xdId: String?): EntityVO<XdUser>(xdId: String?) {
var someName by delegateTo(XdUser::someName)
var someNumber by delegateTo(XdUser::someNumber)
}
and delegateTo should provide delegate which will lookup XdUser by xdId and do get/set value using specified XdUser property. For link/links logic will be more complex but idea can be the same.

Related

Is there a better way to EXTEND a DATA class into multiple sub classes in Kotlin

Let us suppose we have some data classes like following:
data class Order(
orderId: String,
payment: Payment,
customer: String,
...)
data class Payment(
total: BigDecimal,
date: ZoneDateTime,
...)
{
fun getTotal() = total
}
I want to create new types of Order, let's say OrderYellow and OrderBlue to have different new custom fields and also has the possibility to re-override for example the getTotal() method of Payment data class
One way could be a "Java" oriented implementation
abstract class SuperOrder {
abstract val baseOrder: Order,
}
data class OrderYellow(
override var baseOrder: Order,
val status: String,
) : SuperOrder()
data class OrderBlue(
override var baseOrder: Order,
val createdBy: String,
) : SuperOrder()
In my application it is impossible to implement proper inheritance from scratch, since the usage of the Order class is too much spread all over the code base, making impossible a refactor in that sense, hence the above proposed solution seemed to be the best one for the sake of preserving as much existing code as I can.
I also used a lot of time the copy method to maintain immutability of the object and manage the updates on the fields during the completion of the order.
In fact this is a good solution, but in this way I still have the problem that I can't override the getTotal() method of the nested class.
Do you have some ideas? Are there better ways to do it in Kotlin oriented code?
Thanks a lot!

JOOQ: How to add an interface to record class to allow generic insertion of data

Using Jooq 3.12.3, I am generating several Record classes that have similar fields.
I want to add a helper method that will be able to insert records of this type, using a common interface.
Something like:
fun insert(record: MyRecordInterface) {
record.setField1("a")
record.setField2("a")
record.attach(configuration)
record.store()
}
val record1 = Record1()
val record2 = Record2()
insert(record1)
insert(record2)
I am able to add an interface to the record class using matchers strategy, with the common fields accessors, but I am still missing the attach() and store() method that are used in the insert function. Is there a way to do this ?
Just define your interface like this:
interface MyRecordInterface<R : UpdatableRecord<R>> : UpdatableRecord<R> {}

Accessing properties of a Kotlin entity

I'm new to Kotlin, so apologies if I'm not articulating concepts correctly. I have an instance of an Entity
[TestEntity(applicationId=1, timestamp=2018-01-24T18:40:30, issueState=MA, product=test, status=sold, paymentMode=VISA, premium=null)]
I am writing a service that is going to take these keys and use them to create the headers of a report. The keys may change depending on the type of report the user is trying to generate, which will have an impact on the Entity that will be instantiated.
I want to be able to iterate over this Entity so that I can create an array to use for the headers. Any thoughts on how I do this?
I think the cleanest solution is storing values in a map and delegating properties to it.
Don't think you can otherwise iterate over class fields without some verbose getter chain or ugly reflection shenanigans.
For example here you can access map fields as if they were class fields, but can also easily iterate over map.
data class TestEntity(val map : Map<String, Any>){
val appId : Int by map
val timeStamp : Long by map
(... more fields)
}

SerializationException: type not included in serializable type set

In my Google Web Toolkit project, I got the following error:
com.google.gwt.user.client.rpc.SerializationException: Type ‘your.class.Type’ was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.
What are the possible causes of this error?
GWT keeps track of a set of types which can be serialized and sent to the client. your.class.Type apparently was not on this list. Lists like this are stored in .gwt.rpc files. These lists are generated, so editing these lists is probably useless. How these lists are generated is a bit unclear, but you can try the following things:
Make sure your.class.Type implements java.io.Serializable
Make sure your.class.Type has a public no-args constructor
Make sure the members of your.class.Type do the same
Check if your program does not contain collections of a non-serializable type, e.g. ArrayList<Object>. If such a collection contains your.class.Type and is serialized, this error will occur.
Make your.class.Type implement IsSerializable. This marker interface was specifically meant for classes that should be sent to the client. This didn't work for me, but my class also implemented Serializable, so maybe both interfaces don't work well together.
Another option is to create a dummy class with your.class.Type as a member, and add a method to your RPC interface that gets and returns the dummy. This forces the GWT compiler to add the dummy class and its members to the serialization whitelist.
I'll also add that if you want to use a nested class, use a static member class.
I.e.,
public class Pojo {
public static class Insider {
}
}
Nonstatic member classes get the SerializationException in GWT 2.4
I had the same issue in a RemoteService like this
public List<X> getX(...);
where X is an interface. The only implementation did conform to the rules, i.e. implements Serializable or IsSerializable, has a default constructor, and all its (non-transient and non-final) fields follow those rules as well.
But I kept getting that SerializationException until I changed the result type from List to X[], so
public X[] getX(...);
worked. Interestingly, the only argument being a List, Y being an interface, was no problem at all...
I have run into this problem, and if you per chance are using JPA or Hibernate, this can be a result of trying to return the query object and not creating a new object and copying your relavant fields into that new object. Check the following out, which I saw in a google group.
#SuppressWarnings("unchecked")
public static List<Article> getForUser(User user)
{
List<Article> articles = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
try
{
Query query = pm.newQuery(Article.class);
query.setFilter("email == emailParam");
query.setOrdering("timeStamp desc");
query.declareParameters("String emailParam");
List<Article> results = (List<Article>) query.execute(user.getEmail
());
articles = new ArrayList<Article>();
for (Article a : results)
{
a.getEmail();
articles.add(a);
}
}
finally
{
pm.close();
}
return articles;
}
this helped me out a lot, hopefully it points others in the right direction.
Looks like this question is very similar to what IsSerializable or not in GWT?, see more links to related documentation there.
When your class has JDO annotations, then this fixed it for me (in addition to the points in bspoel's answer) : https://stackoverflow.com/a/4826778/1099376

An alternative way to use Azure Table Storage?

I'd like to use for table storage an entity like this:
public class MyEntity
{
public String Text { get; private set; }
public Int32 SomeValue { get; private set; }
public MyEntity(String text, Int32 someValue)
{
Text = text;
SomeValue = someValue;
}
}
But it's not possible, because the ATS needs
Parameterless constructor
All properties public and
read/write.
Inherit from TableServiceEntity;
The first two, are two things I don't want to do. Why should I want that anybody could change some data that should be readonly? or create objects of this kind in a inconsistent way (what are .ctor's for then?), or even worst, alter the PartitionKey or the RowKey. Why are we still constrained by these deserialization requirements?
I don't like develop software in that way, how can I use table storage library in a way that I can serialize and deserialize myself the objects? I think that as long the objects inherits from TableServiceEntity it shouldn't be a problem.
So far I got to save an object, but I don't know how retrieve it:
Message m = new Message("message XXXXXXXXXXXXX");
CloudTableClient tableClient = account.CreateCloudTableClient();
tableClient.CreateTableIfNotExist("Messages");
TableServiceContext tcontext = new TableServiceContext(account.TableEndpoint.AbsoluteUri, account.Credentials);
var list = tableClient.ListTables().ToArray();
tcontext.AddObject("Messages", m);
tcontext.SaveChanges();
Is there any way to avoid those deserialization requirements or get the raw object?
Cheers.
If you want to use the Storage Client Library, then yes, there are restrictions on what you can and can't do with your objects that you want to store. Point 1 is correct. I'd expand point 2 to say "All properties that you want to store must be public and read/write" (for integer properties you can get away with having read only properties and it won't try to save them) but you don't actually have to inherit from TableServiceEntity.
TableServiceEntity is just a very light class that has the properties PartitionKey, RowKey, Timestamp and is decorated with the DataServiceKey attribute (take a look with Reflector). All of these things you can do to a class that you create yourself and doesn't inherit from TableServiceEntity (note that the casing of these properties is important).
If this still doesn't give you enough control over how you build your classes, you can always ignore the Storage Client Library and just use the REST API directly. This will give you the ability to searialize and deserialize the XML any which way you like. You will lose the all of the nice things that come with using the library, like ability to create queries in LINQ.
The constraints around that ADO.NET wrapper for the Table Storage are indeed somewhat painful. You can also adopt a Fat Entity approach as implemented in Lokad.Cloud. This will give you much more flexibility concerning the serialization of your entities.
Just don't use inheritance.
If you want to use your own POCO's, create your class as you want it and create a separate tableEntity wrapper/container class that holds the pK and rK and carries your class as a serialized byte array.
You can use composition to achieve what you want.
Create your Table Entities as you need to for storage and create your POCOs as wrappers on those providing the API you want the rest of your application code to see.
You can even mix in some interfaces for better code.
How about generating the POCO wrappers at runtime using System.Reflection.Emit http://blog.kloud.com.au/2012/09/30/a-better-dynamic-tableserviceentity/