how to create node path similar to the sling job creates - aem

AEM6.2 - I want to create a node hierarchy similar to the sling creates under "/var/eventing/..".
It should be based as "var/eventing/xx/year/month/date/hours/minutes/seconds/milisenconds/<>"
How do you suggest - to create each folder node by iterating the date format "YYYY/MM/dd/hh/mm/ss/SSS" ?
Or is there any other best way ?

You can use ResourceUtil.getOrCreateResource method. Pass the complete path you need and it will create all the sub directories if it does not already exist
String VAR_DATA_ROOT = "/var/eventing";
Date currentDate = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/YYYY/MM/dd/hh/mm/ss/SSS");
String bucketPath = VAR_DATA_ROOT+simpleDateFormat.format(currentDate);
Resource bucketResource = ResourceUtil.getOrCreateResource(resourceResolver,bucketPath,null,null,false);
//save the data under bucketResource
resourceResolver.commit();

Related

Apache Tika - Parsing and extracting only metadata without reading content

Is there a way to configure the Apache Tikka so that it only extracts the metadata properties from the file and does not access the content of the file. ? We need a way to do this so as to avoid reading the entire content in larger files.
The code to extract we are using is as follows:
var tikaConfig = TikaConfig.getDefaultConfig();
var metadata = new Metadata();
AutoDetectParser parser = new AutoDetectParser(tikaConfig);
BodyContentHandler handler = new BodyContentHandler();
using (TikaInputStream stream = TikaInputStream.get(new File(filename), metadata))
{
parser.parse(stream, handler, metadata, new ParseContext());
Array metadataKeys = metadata.names();
Array.Sort(metadataKeys);
}
With the above code sample, when we try to extract the metadata even the content is being read. We would need a way to avoid the same.

How to return a normal javascript date object?

Is it possible to return a javascript date object from this? I have an text input field with the calendar and want to return a standard date object from it's value... Is this possible?
Is this what you are looking for?
var date = '2016-06-12'; // Can be document.getElementById('myField').value that points to your date field.
var date_parts = date.split('-');
var date_obj = new Date(date_parts[0],date_parts[1],date_parts[2]);
console.log(date_obj);
You can also simply use
new Date(document.getElementById('myField').value)
and see if it works. The date function is smart enough to parse based on browser's locale. This should work for time as well. Eg. new Date('2016-06-12 04:15:30')

MEF - List of DLLs to load

We have many products that will share common DLLs. For a product, I would like to indicate a specific list of DLLs to include in the catalog. I know I can do this:
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MainWindow).Assembly));
string fullPath = Path.Combine(#"D:\Folder\With\Plugins", "SomePlugin.dll");
Assembly dll = Assembly.LoadFile(fullPath);
ComposablePartCatalog assemblyCatalog = new AssemblyCatalog(dll);
catalog.Catalogs.Add(assemblyCatalog);
_container = new CompositionContainer(catalog);
_container.ComposeParts(this);
Would looping in the middle part for each specific file be the best method?
MEF provides a DirectoryCatalog. All you have to do is point the directory that contains the plugins and it will handle the rest, no loop necessary. Something like this:
string fullPath = Path.Combine(#"D:\Folder\With\Plugins");
var catalog = new DirectoryCatalog(fullPath);
_container = new CompositionContainer(catalog);
_container.ComposeParts(this);

Passing dynamic value gets failed in Mongodb using java script

I have to copy the data from one collection to another collection based on a date. Here date is calculated as yesterday date dynamically and working properly.
If i pass the dynamic date value as /$yesterday/ to mongo find method, Its getting failed.
Assume data_timestamp format is 2013-08-20 17:04:40.633 and trying to get the result by like query.
Sample JS Code:
db=db.getSiblingDB('masterdb')
$today = new Date();
$yesterday = new Date($today);
$yesterday.setDate($today.getDate() - 1);
var $dd = $yesterday.getDate();
var $mm = $yesterday.getMonth()+1;
var $yyyy = $yesterday.getFullYear();
if($dd<10){$dd='0'+dd} if($mm<10){$mm='0'+$mm} $yesterday = $yyyy+'-'+$mm+'-'+$dd;
db.mastercollection.find( { "data_timestamp": /$yesterday/ } ).forEach( function(x){db.newcollection.insert(x)} );
Is any other way to pass dynamic value without using '$' symbol?
Please share your valuable comments
Thanks in advance...
Ramesh Kasi
The way you're doing your query now, I'm pretty sure that /$yesterday/ is being interpreted as a regular expression matching strings starting with "yesterday". A better approach would be to use the $regex operator so that you can pass in a javascript variable that holds the regular expression you hope to match.

Enterprise Library Fluent API and Rolling Log Files Not Rolling

I am using the Fluent API to handle various configuration options for Logging using EntLib.
I am building up the loggingConfiguration section manually in code. It seems to work great except that the RollingFlatFileTraceListener doesn't actually Roll the file. It will respect the size limit and cap the amount of data it writes to the file appropriately, but it doesn't not actually create a new file and continue the logs.
I've tested it with a sample app and the app.config and it seems to work. So I'm guess that I am missing something although every config option that seems like it needs is there.
Here is the basics of the code (with hard-coded values to show a config that doesn't seem to be working):
//Create the config builder for the Fluent API
var configBuilder = new ConfigurationSourceBuilder();
//Start building the logging config section
var logginConfigurationSection = new LoggingSettings("loggingConfiguration", true, "General");
logginConfigurationSection.RevertImpersonation = false;
var _rollingFileListener = new RollingFlatFileTraceListenerData("Rolling Flat File Trace Listener", "C:\\tracelog.log", "----------------------", "",
10, "MM/dd/yyyy", RollFileExistsBehavior.Increment,
RollInterval.Day, TraceOptions.None,
"Text Formatter", SourceLevels.All);
_rollingFileListener.MaxArchivedFiles = 2;
//Add trace listener to current config
logginConfigurationSection.TraceListeners.Add(_rollingFileListener);
//Configure the category source section of config for flat file
var _rollingFileCategorySource = new TraceSourceData("General", SourceLevels.All);
//Must be named exactly the same as the flat file trace listener above.
_rollingFileCategorySource.TraceListeners.Add(new TraceListenerReferenceData("Rolling Flat File Trace Listener"));
//Add category source information to current config
logginConfigurationSection.TraceSources.Add(_rollingFileCategorySource);
//Add the loggingConfiguration section to the config.
configBuilder.AddSection("loggingConfiguration", logginConfigurationSection);
//Required code to update the EntLib Configuration with settings set above.
var configSource = new DictionaryConfigurationSource();
configBuilder.UpdateConfigurationWithReplace(configSource);
//Set the Enterprise Library Container for the inner workings of EntLib to use when logging
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
Any help would be appreciated!
Your timestamp pattern is wrong. It should be yyy-mm-dd instead of MM/dd/yyyy. The ‘/’ character is not supported.
Also, you could accomplish your objective by using the fluent configuration interface much easier. Here's how:
ConfigurationSourceBuilder formatBuilder = new ConfigurationSourceBuilder();
ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();
builder.ConfigureLogging().LogToCategoryNamed("General").
SendTo.
RollingFile("Rolling Flat File Trace Listener")
.CleanUpArchivedFilesWhenMoreThan(2).WhenRollFileExists(RollFileExistsBehavior.Increment)
.WithTraceOptions(TraceOptions.None)
.RollEvery(RollInterval.Minute)
.RollAfterSize(10)
.UseTimeStampPattern("yyyy-MM-dd")
.ToFile("C:\\logs\\Trace.log")
.FormatWith(new FormatterBuilder().TextFormatterNamed("textFormatter"));
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
var writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
DateTime stopWritingTime = DateTime.Now.AddMinutes(10);
while (DateTime.Now < stopWritingTime)
{
writer.Write("test", "General");
}