Thursday, 14 August 2014

Fixing 1 property on nodes in content

First of all locate all of the data I need :-

path=/content/catalogs/XXX/v1/master/collections
1_property=sling:resourceType
1_property.value=commerce/components/section
2_property=@target/pageInformation
2_property.operation=exists
2_property.value=true
p.limit=-1
p.hits=selective
p.properties=jcr:path target/pageInformation

This gives me a JSON output.  Put the JSON output in a file and transform the file :

perl -pe 's/},{/\n/g' < raw_data.json > raw_data.txt

I now have line separators.  Edit the raw_data.txt and remove the first line, relating to "success" of the query.

Run the following Perl program to generate curl commands which will put the data on to the target CQ instance :-

#!/usr/bin/perl

sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

open (FILE, 'raw_data.txt');
while (<FILE>)
{
chomp;

($jcr_path_data, $page_information_data) = split("\",\"");
($rubbish1, $target_path) = split("\":\"", $jcr_path_data);
($rubbish2, $page_information) = split("\":\"", $page_information_data);

$target_path =~ s/"//g;
$page_information =~ s/"//g;

if ($page_information =~ /^''''''''/)
{
    $page_information =~ s/^''''''''/''''en''''/;
}

$TARGET_HOST="localhost:4502";
$TARGET_USER="admin";
$TARGET_PASS="admin";

$CURL_CMD = "/usr/bin/curl -u \"" . $TARGET_USER . ":" . $TARGET_PASS . "\" -F \"_charset_=UTF-8\" -F \"pageInformation=" . $page_information . "\" \"http://" . $TARGET_HOST . "$target_path/target\"";

print ($CURL_CMD) . "\n";
#system($CURL_CMD);
print "\n"; print "\n";
}

Redirect the output of this to a separate file & then execute that file.  This way we have a record of the commands that were executed.

Note, that the form field name "_charset_=UTF-8" allows you to post accented characters.  This way you can use curl to HTTP POST data in the correct charset to Apache Sling.  See the "Character Encoding" section of : Apache Sling - Request Parameter Handling in Sling