Hybris DataHub INVALID_LOCALE Exception - composition

I have localized raw data item baseName. I want to send localized raw data item to DataHub. I read many documents, it writes send localized raw attribute value but I couldn't find the format of the localized attribute value. In the composition, it throws INVALID_LOCALE exception.
I am sending value for baseName, but how can I localized "XYZ"?
RawFragmentData rawFragmentData = new RawFragmentData();
final Map<String, String> line = new HashMap<>();
........
line.put("baseName", "XYZ");
........
rawFragmentData.setValueMap(line);
rawFragmentData.setType(type);
rawFragmentData.setDataFeedName(feedName);
rawFragmentData.setExtensionSource(Constants.DATAHUB_EXTENSION_SOURCE);
return rawFragmentData;

e.g OOTB :
DefaultPartnerContributor.Java :-
row.put(PartnerCsvColumns.COUNTRY_ISO_CODE, address.getCountry());
Same way you might have languageColumn for it, so just pass language value to it.

Related

Jira Rest API - Problems to set custom fields

I try to set the field for Testcases in a Testplan. The value I get when reading it is JSONArray.
But when I write the very same JSONArray I extract to a new created Testplan, I get an error message.
Exception in thread "main" com.atlassian.jira.rest.client.api.domain.input.CannotTransformValueException: Any of available transformers was able to transform given value. Value is: org.codehaus.jettison.json.JSONArray: ["SBNDTST-361","SBNDTST-360","SBNDTST-358","SBNDTST-359"]
at com.atlassian.jira.rest.client.api.domain.input.ValueTransformerManager.apply(ValueTransformerManager.java:83)
at com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder.setFieldValue(IssueInputBuilder.java:134)
My method to set the field is this:
public void updateIssue(String issueKey, String fieldId, Object fieldValue) {
IssueInput input = new IssueInputBuilder()
.setFieldValue(fieldId, fieldValue)
.build();
restClient.getIssueClient()
.updateIssue(issueKey, input)
.claim();
The value for the fieldId is "customfield_17473". There is very little documentation on this. Does anyone have an idea how to proceed?
I found the solution by trial and error.
When I send an ArrayList it works.

Google diff-match-patch : How to unpatch to get Original String?

I am using Google diff-match-patch JAVA plugin to create patch between two JSON strings and storing the patch to database.
diff_match_patch dmp = new diff_match_patch();
LinkedList<Patch> diffs = dmp.patch_make(latestString, originalString);
String patch = dmp.patch_toText(diffs); // Store patch to DB
Now is there any way to use this patch to re-create the originalString by passing the latestString?
I google about this and found this very old comment # Google diff-match-patch Wiki saying,
Unpatching can be done by just looping through the diff, swapping
DIFF_INSERT with DIFF_DELETE, then applying the patch.
But i did not find any useful code that demonstrates this. How could i achieve this with my existing code ? Any pointers or code reference would be appreciated.
Edit:
The problem i am facing is, in the front-end i am showing a revisions module that shows all the transactions of a particular fragment (take for example an employee details), like which user has updated what details etc. Now i am recreating the fragment JSON by reverse applying each patch to get the current transaction data and show it as a table (using http://marianoguerra.github.io/json.human.js/). But some JSON data are not valid JSON and I am getting JSON.parse error.
I was looking to do something similar (in C#) and what is working for me with a relatively simple object is the patch_apply method. This use case seems somewhat missing from the documentation, so I'm answering here. Code is C# but the API is cross language:
static void Main(string[] args)
{
var dmp = new diff_match_patch();
string v1 = "My Json Object;
string v2 = "My Mutated Json Object"
var v2ToV1Patch = dmp.patch_make(v2, v1);
var v2ToV1PatchText = dmp.patch_toText(v2ToV1Patch); // Persist text to db
string v3 = "Latest version of JSON object;
var v3ToV2Patch = dmp.patch_make(v3, v2);
var v3ToV2PatchTxt = dmp.patch_toText(v3ToV2Patch); // Persist text to db
// Time to re-hydrate the objects
var altV3ToV2Patch = dmp.patch_fromText(v3ToV2PatchTxt);
var altV2 = dmp.patch_apply(altV3ToV2Patch, v3)[0].ToString(); // .get(0) in Java I think
var altV2ToV1Patch = dmp.patch_fromText(v2ToV1PatchText);
var altV1 = dmp.patch_apply(altV2ToV1Patch, altV2)[0].ToString();
}
I am attempting to retrofit this as an audit log, where previously the entire JSON object was saved. As the audited objects have become more complex the storage requirements have increased dramatically. I haven't yet applied this to the complex large objects, but it is possible to check if the patch was successful by checking the second object in the array returned by the patch_apply method. This is an array of boolean values, all of which should be true if the patch worked correctly. You could write some code to check this, which would help check if the object can be successfully re-hydrated from the JSON rather than just getting a parsing error. My prototype C# method looks like this:
private static bool ValidatePatch(object[] patchResult, out string patchedString)
{
patchedString = patchResult[0] as string;
var successArray = patchResult[1] as bool[];
foreach (var b in successArray)
{
if (!b)
return false;
}
return true;
}

Velocity: Keep carriage returns and tabs

I'm using Velocity and JavaMail with spring to create mails for a Java app.
I save a Hibernate #Lob in database.
My clob is saved with carriage returns and tabulations in db:
but when I receive the mail my text is not formatted anymore:
My code is quite simple:
Map model = new HashMap();
model.put("monitoringError", baseMonitoringError);
model.put("businessCode", businessCode);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "velocity/crashMailTemplate.vm", "UTF-8", model);
from the template:
<p><strong>Stacktrace:</strong></p>
<p><em><span class="crayon-i ">${monitoringError.stacktrace}</span> </em></p>
...and the baseMonitoringError bean is a Hibernate Entity with
#Lob
#Column
private String stacktrace;
How should I do to keep the formatted original text?
Thanks
Try with:
${monitoringError.stacktrace.replace("\n","<br/>")}
or, depending on your platform:
${monitoringError.stacktrace.replace("\r\n","<br/>")}

How iterate all Property of a node and update the value of each property and save in repository in cq5

My cq5 Content Structure is....
Content
---mywebsite
------base
-----us
--- en
----pageOne
----pageTwo
----pageThree
----pageFour
----cq:content
----par
----pageFourNew
"pageFourNew" has around 500 Properties.
Now I need to get all the properties of "pageFourNew" and to update their value.
For example if I have:
property=prop1
value = prop1 value
I want to do value = value+"some string value append" and save it on the repository.
I want to do this in a programmatically way.
Please share if you have any solution or idea.
You can use PropertyIterator to iterate through all the properties, setProperty() method of node api to set the new value and jcr session to persist the value to get this done. Sample code:
PropertyIterator propertyIterator = pageFourNew.getProperties();
while (propertyIterator.hasNext()) {
Property property = propertyIterator.nextProperty();
pageFourNew.setProperty(property.getName(),
property.getValue().getString() + "");
jcrSession.save();}
You can easily do this as suggested above at JCR level. But as per CQ practices and this blog
It is better practice to operate at Sling level and NOT JCR level, just to avoid overhead of managing the resources. You can use below code which works:
Resource resource = pageFourNew; // assuming you are getting sling resource properly
ModifiableValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
for(String key : valueMap.keySet()) {
String value = valueMap.get(key, String.class);
value = value + "additional texts";
valueMap.put(key, value);
}
resource.getResourceResolver().commit();
This is cleaner approach.

An interesting Restlet Attribute behavior

Using Restlet 2.1 for Java EE, I am discovering an interesting problem with its ability to handle attributes.
Suppose you have code like the following:
cmp.getDefaultHost().attach("/testpath/{attr}",SomeServerResource.class);
and on your browser you provide the following URL:
http://localhost:8100/testpath/command
then, of course, the attr attribute gets set to "command".
Unfortunately, suppose you want the attribute to be something like command/test, as in the following URL:
http://localhost:8100/testpath/command/test
or if you want to dynamically add things with different levels, like:
http://localhost:800/testpath/command/test/subsystems/network/security
in both cases the attr attribute is still set to "command"!
Is there some way in a restlet application to make an attribute that can retain the "slash", so that one can, for example, make the attr attribute be set to "command/test"? I would like to be able to just grab everything after testpath and have the entire string be the attribute.
Is this possible? Someone please advise.
For the same case I usually change the type of the variable :
Route route = cmp.getDefaultHost().attach("/testpath/{attr}",SomeServerResource.class);
route.getTemplate().getVariables().get("attr") = new Variable(Variable.TYPE_URI_PATH);
You can do this by using url encoding.
I made the following attachment in my router:
router.attach("/test/{cmd}", TestResource.class);
My test resource class looks like this, with a little help from Apache Commons Codec URLCodec
#Override
protected Representation get() {
try {
String raw = ResourceWrapper.get(this, "cmd");
String decoded = new String(URLCodec.decodeUrl(raw.getBytes()));
return ResourceWrapper.wrap(raw + " " + decoded);
} catch(Exception e) { throw new RuntimeException(e); }
}
Note my resource wrapper class is simply utility methods. The get returns the string of the url param, and the wrap returns a StringRepresentation.
Now if I do something like this:
http://127.0.0.1/test/haha/awesome
I get a 404.
Instead, I do this:
http://127.0.0.1/test/haha%2fawesome
I have URLEncoded the folder path. This results in my browser saying:
haha%2fawesome haha/awesome
The first is the raw string, the second is the result. I don't know if this is suitable for your needs as it's a simplistic example, but as long as you URLEncode your attribute, you can decode it on the other end.