the best way to clone hybris beans? - sap-commerce-cloud

I needed to clone the hybris bean "ProductData", and I couldn't find something OOTB from Hybris that helps with this.
And since hybris beans don't implement Cloneable and don't have constructors, it seems to me that there are only two ways left, either by creating a custom cloning method witch require a lot of dev, or through serialization/deserialization.
Is there another way to do that? and does Hybris provides something OOTB for this? or else is the serialization/deserialization a good approach (in term of performance) for this since there is the SerializationUtils from apache that I can use?

Hi You can directly Inject dataMapper bean in your controller or in service.
import de.hybris.platform.webservicescommons.mapping.DataMapper;
#Autowired
private DataMapper dataMapper;
dataMapper.map(e, ProductData.class)

Based on which hybris version you use, you can also use ma.glasnost.orika.MapperFactory like:
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
MapperFacade mapper = mapperFactory.getMapperFacade();
ProductData clonedData = mapper.map(productData, ProductData.class);

Related

Usage of implementsInterface element on entities in guidewire

I would like to know why do we use implementsInterface element in entities. I know one example where they use it to make it as assignable entity. But I could not understand what other purpose and how/why it is being used in entities.
Example: Injuryincident entity has claimantsupplier and coveragesupplier interface
I like to see it from this prespective, simplified and assuming that you have some java background:
As you probably already know it, having an entity means in the end of the day, having a Java class... Well, by using the implementsInterface element in your entity, is similar to implement an interface in you java class.
Here you have a quick example...
Consider the following:
MyEntiti.eti
<?xml version="1.0"?>
<entity
xmlns="http://guidewire.com/datamodel"
entity="MyEntity"
table="myentity"
type="retireable"/>
AnInterface.gs
package mypkg
interface AnInterface {
function doSomething()
}
AnInterfaceImpl.gs
package mypkg
class AnInterfaceImpl implements AnInterface {
override function doSomething() {
print("Hello!")
}
}
Image that you need MyEntity to have the ability of "doSomething", you just need to add the implementsInterface:
<?xml version="1.0"?>
<entity
xmlns="http://guidewire.com/datamodel"
entity="MyEntity"
table="myentity"
type="retireable">
<implementsInterface
iface="mypkg.AnInterface"
impl="mypkg.AnInterfaceImpl"/>
</entity>
By doing that, the following code must work:
var myEntity = new MyEntity()
myEntity.doSomething() //this will call the method defined in the interface-implementation
And even better, you migth let you implementation to recognize the related object of MyEntity and use it as per your needs:
package mypkg
class AnInterfaceImpl implements AnInterface {
private final var _relatedEntity : MyEntity
construct(relatedTo : MyEntity) {
_relatedEntity = relatedTo
}
override function doSomething() {
var createUser = _relatedEntity.CreateUser // you can accees to whatever you need
print("Hello!, this is the related instace of MyEntity: ${_relatedEntity}")
}
}
Hope it helps, regards!
I won't be repeating the other answer describing how it works, but I would like to mention how implementing an interface on an entity is different (and serves different purposes) compared to using enhancements.
On basic level both approaches let you add extra functionality to your entity classes. In most cases what you really want to do is just create/expand an enhancement - they are easier to write, more convenient to modify and just as effective when all you want is to just add a new function or calculated property.
When you implement an interface, you're bringing in some more serious guns. While this approach takes more work and requires creation of several files (not to mention modifying the entity itself), it gives you two important advantages over the enhancement mechanism:
The same interface can be implemented by several entities (typically each having its own implementation class) as well as non-entity classes. Objects of all such classes can then be used interchangeably in contexts expecting the interface (you can create an array of entity instances of several entities and even gosu-only wrappers/temporary objects and present it comfortably in the UI).
You can leverage polymorphism. While enhancement functions can't be overridden, the interface implementations allow you full flexibility of polymorphic OOP. You can, for example, set up a default "do nothing" implementation on high level entity that you intend to use and then add more meaningful implementations for specific subtypes meant to really make use of the new functionality.
It does have some overhead and complicates things, however. As mentioned - Enhancements are typically simpler. In practice you should ask yourself whether the extra effort of creating and implementing the interface is worth it - in many cases even situations seemingly calling for polymorphism can be handled well enough by a simple switch typeof this in the enhancement to provide all the necessary type-based logic.
In personal experience I've used interfaces in quite a few situations, but Enhancements are my first choice in overwhelming majority of cases.
As a final note I'd like to mention a delegate entity. If what you want to add to some unrelated entities is not functionality but Properties with underlying database fields, creating a delegate entity and "implement" it with the desired standalone entities. A delegate entity does work a bit like an interface (you can use entity objects implementing the delegate interchangeably in situations where the delegate is expected) and you can set-up both interface implementation and enhancements on delegate level as well.

How to order/sort my collections / relationships?

I've just started using Jhipster for a simple project with a very simple datamodel (so far).
I have a question regarding the generated code for the one-to-many relationship:
Is it possible to generate with List instead of a Set, so I can have my child-items ordered?
If no, what is the best solution to solve my problem? I see 2 ways:
Change the generated code manually to use a List and then use liquibase (mvn liquibase:diff) to update my database ?
Have an attribute on the child-item to handle the order ?
What is the best way to handle the "problem" ?
Best regards
Martin Elkkjær
You can use the Spring #OrderBy annotation to sort your sets by the child entity. See http://www.objectdb.com/api/java/jpa/OrderBy
#Entity
public class Person {
...
#OrderBy("zipcode.zip, zipcode.plusFour")
public Set<Address> getResidences() {...};
...
}
I'd also recommend the following blog that explains how Sets/Lists differ for Hibernate and JPA: https://vladmihalcea.com/hibernate-facts-favoring-sets-vs-bags/ (where I found the answer originally)

How to override default spring integration configuration

For example:
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
<int-jdbc:inbound-channel-adapter.......
I want this to use my custom implementation, so i need to know
1) What class it uses (so i can extend it and override whatever)
2) than how do i tell it to use my new class rather than the default one.
is this possible?
Thanks
There is currently no way to inject your own implementation using the XML namespace, but you can simply wire up <bean/> definitions. After all, that's all that the namespace parser does.
In this case, you need a bean of class SourcePollingChannelAdapterFactoryBean which needs PollerMetadata and a source (and creates a SourcePollingChannelAdapter), with the source being a bean of class JdbcPollingChannelAdapter (which is a MessageSource).
So you would subclass the JdbcPollingChannelAdapter.
If you think your implementation will have wide appeal, please open a JIRA issue and consider contributing it.

Can you explain Service-builder concept?

It's easier to generate tables and default classes with liferay's service builder.
But it is bit difficult for me to understand the hierarchy of classes generated by it.
Also the exact use of each class other than modifying LocalServiceImpl class as per our need by adding our own functionality.
I have gone through Service Builder doc. So I got some basic knowledge but would like to get more on this.
Even the link "Liferay Service Builder" was useful but outdated with current Liferay version. So Does anyone have update on above link or nice info explaining hierarchy with example/diagram?.
Any help is appreciated.
Thanks.
To implement the foreign key concept in liferay, you have to apply your own coding logic since the implementation of FK are not available in service builder. So I guess you need to put some code.
Read some forums related to service builder on liferay website. It will give you better exposure.
Read Liferay In Action. It is a good book
Here is What I obeserved In Liferay 6.1.
com.liferay.portlet.p_name.model:
This has interfaces and classes related to (table entity related to) this portlet.
For example com.liferay.portlet.announcements.model will have
Interfaces:
AnnouncementsEntry
AnnouncementsEntryModel
AnnouncementsDelivery
AnnouncementsDeliveryModel
AnnouncementsFlag
AnnouncementsFlagModel
Classes:
And In classes section, You will find wrapper classes and soap classes for each entity like:
AnnouncementEntryWrapper
AnnouncementEntrySoap
AnnouncementFlagWrapper
AnnouncementFlagSoap
AnnouncementDeliveryWrapper
AnnouncementDeliverySoap
Also for few Entity, you will find Classes for Constant entries like
AnnouncementsEntryConstants, AnnouncementsFlagConstants etc.
com.liferay.portlet.p_name.service:
This has classes and interfaces related to (table entity related to) this portlet.
For example com.liferay.portlet.announcements.service will have
Interfaces:
AnnouncementsEntryService
AnnouncementsEntryLocalService
AnnouncementsDeliveryService
AnnouncementsDeliveryLocalService
AnnouncementsFlagService
AnnouncementsFlagLocalService
Classes:
And In classes section you will have Util and Wrapper classes.
AnnouncementsEntryServiceUtil
AnnouncementsEntryServiceWrapper
AnnouncementsEntryLocalServiceUtil
AnnouncementsEntryLocalServiceWrapper
and so on.....
So In short following is the structure I observed in Liferay
[In Model, I:+Model,(ModelName), C:Soap, Wrapper and Constants]
[In Service, I:Service,LocalService ; C:ServiceUtil,ServiceWrapper,LocalServiceUtil and LocalServiceWrapper]
[In Persistence, I:Finder(BlogsEntryFinder),Persistence(BlogsEntryPersistence); C:Util(BlogsEntryUtil, BlogsEntryFinderUtil)]
Note:
I --> Interface
C --> Class

RetryPolicy in Subsonic

I am using Subsonic with a SqlAzure database and it's working great. I would like to improve my application by implementing the suggested best practice mentioned in this blog article.
According to the article, I could do something like:
var sqlAzureRetryPolicy = ... code omitted ...;
return sqlAzureRetryPolicy.ExecuteAction<IEnumerable<Product>>(() =>
{
// Invoke a LINQ query.
return result;
});
However this would mean that I would have to copy and paste this code snippet all over my solution and I think it would be tedious and error prone, other members of my team could forget, etc. I don't think it's the best solution and I'm wondering if there's a better way.
Anybody has suggestions on how to do it?
Have you looked into implementing an extension method? You can "add" a method to existing classes (including IEnumerable and IQueryable in your case) to make it look like the method is part of the class. Extension methods can be useful to centralize this type of code. Here is a simple article showing how to extend the string class: http://www.developer.com/net/csharp/article.php/3592216/Using-the-New-Extension-Methods-Feature-in-C-30.htm
I personally use extension methods (I created a TryOpen and TryExecuteReader) to do just what you are asking for, but against SqlCommand and SqlConnection classes. So my code sample won't help you for LINQ.

Resources