Multiple File search in varying level of directories in perl - file

I have a directory structures as follows. I want to have only the first path, not the paths which are present after the first file in the report.
I also don't want to check further in that report after getting first file, thus want to save time.
structure for directory:
Report 1
A--B--C--D--E--abc.txt--->needed this
A--B--C--D--E--F--abc.txt avoid this
Report 2
A--B--C--D--E--Q--R--abc.txt needed this, as it is single in its report.
Report 3
A--H--I--J--abc.txt --needed this
Report 4
A--B--C--D--M--abc.txt needed this
A--B--C--D--M--N--abc.txt avoid this
.
.
.
.
millions of such reports.
Directory A contains millions of reports. Each report contains multiple files and subdirectories. Each report have abc.txt in one of the path present
and same path post abc.txt level ,may contain other subdirectories inside the path which also have have abc.txt.
Note:
Reports are of varying level of subdirectories
     
open my $fh, '-|', 'find', $maindirectory, '-type','d' or die "cant open pipes $! \n";
            while (<$fh>) {
                my $dirpath = $_;
                chomp $dirpath;
                if(-d $dirpath) {
                    $filewithpath = File::Spec->catfile( $dirpath, "abc.txt" );
                    if (-e $filewithpath) {
                        push #patharray, $filewithpath;
                    }
                }
}

I think you want the abc.txt files
which are nearest to the
main directory for a common initial path.
That is, you want to avoid looking for
A/B/C/../F/abc.txt if A/B/C/abc.txt
has been found.
This criteria will select files A/H/I/J/abc.txt,
A/B/C/D/M/abc.txt, A/B/C/D/E/abc.txt for your
sample directory tree.
And not A/B/C/D/E/Q/R/abc.txt,
which you have marked as needed in your sample,
because the file A/B/C/D/E/abc.txt has already
been found above it in the directory hierarchy.
You can do this in perl using:
use strict;
use warnings;
use File::Find;
my $maindirectory = "A";
#replace with actual main directory name
File::Find::find(
sub {
if ( -d && -f "$_/abc.txt" ) {
$File::Find::prune = 1;
process_path_of_needed_file("$File::Find::name/abc.txt");
}
},
$maindirectory
);
See https://perldoc.perl.org/File/Find.html

Related

Docusign embedded sigin - Merge field is not working

I am using embedding signing from salesforce using apex toolkit. I could not merge fields from salesforce. I am trying to prepopulate firstname and lastname. I tried with customtabs and customfields. Custom tab is repeating the same value for all the tabs and custom field is not working. Can someone help pls.
contact c =[select firstName,lastname, Envelope_Id__c from contact where id = :currentUser.contactId];
string EnvelopeId='';
Id mySourceId = currentUser.ContactId; // The ID of the initiating Salesforce object
String conStr = (String) currentUser.ContactId + '~Contact';
ContractType__mdt ContractType= [SELECT label, Envelope_Configuration__c,External_Document_Id__c
FROM ContractType__mdt where Envelope_Configuration__c =:application.Envelope_Configuration__c limit 1];
string templateId= ContractType.External_Document_Id__c;
/*
dfsle.Envelope dsEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
new dfsle.Entity(mySourceId)) // The initiating Salesforce entity--current SF user (salesperson)
.withDocuments(new List<dfsle.Document> {
dfsle.Document.fromTemplate(dfsle.UUID.parse(templateId), description)
})
.withRecipients(new List<dfsle.Recipient> {
dfsle.Recipient.newEmbeddedSigner() // An embedded signer
}
);
if(!Test.isRunningTest()){
// Send the envelope.
dsEnvelope = dfsle.EnvelopeService.sendEnvelope(
dsEnvelope, // The envelope to send
true // Send now?
);
EnvelopeId= String.valueOf(dsEnvelope.docuSignId);
}*/
dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
new dfsle.Entity(mySourceId));
// The initiating Salesforce entity (an opportunity).
dfsle.Tab myTextFNAMETab = new dfsle.TextTab()
.withRequired(true) // Signer must enter value
.withValue(c.firstName)
.withReadOnly(false)
.withName('FName')
// .withAnchor(new dfsle.Tab.Anchor('Legal Name', true, true, null, true, true, 'pixels', 60, -4));
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
149, // X position
240, // Y position
80, // 100 pixels wide
null)); // Default height
dfsle.Tab myTextLNAMETab = new dfsle.TextTab()
.withRequired(true) // Signer must enter value
.withValue(c.lastName)
.withReadOnly(false)
.withName('LName')
// .withAnchor(new dfsle.Tab.Anchor('Legal Name', true, true, null, true, true, 'pixels', 60, -4));
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
230, // X position
240, // Y position
80, // 100 pixels wide
null)); // Default height
dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
myRecipient2.withTabs(new List<dfsle.Tab> {myTextLNAMETab});
//dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
//add Recipient to the Envelope
myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient2});
myEnvelope = myEnvelope.withEmail('Testt Email Subject', 'Test Email Message');
//myTemplateId contains the DocuSign Id of the DocuSign Template
dfsle.UUID myTemplateId = dfsle.UUID.parse(templateId);
//create a new document for the Envelope
dfsle.Document myDocument = dfsle.Document.fromTemplate(
myTemplateId, // templateId in dfsle.UUID format
'Enrollment Agreement'); // name of the template
dfsle.CustomField myField = new dfsle.CustomField ('text', 'DSFSSourceObjectId', conStr, null, false, false);
//add document to the Envelope
myEnvelope = myEnvelope.withDocuments(new List<dfsle.Document> { myDocument })
.withCustomFields(new List<dfsle.CustomField> {myField});
if(!Test.isRunningTest()){
myEnvelope = dfsle.EnvelopeService.sendEnvelope(
myEnvelope, // The envelope to send
true); // Send now?
I'm assuming you're trying to populate existing fields in your template, if that's the case:
// This will populate all the fields with data label name 'FName':
dfsle.Tab prefill_firstname = new dfsle.TextTab()
.withValue(myContact.firstName)
.withDataLabel('FName');
// This will populate all the fields with data label name 'LName':
dfsle.Tab prefill_lastname = new dfsle.TextTab()
    .withValue(myContact.lastName)
    .withDataLabel('LName');
// Otherwise, if you just want to a create new recipient's first & last name tabs instead of populating field:
dfsle.Tab New_firstname_tab = new dfsle.FirstNameTab()
      .withName('AdditionLastName')
      .withPosition(new dfsle.Tab.Position(
      1, // The document to use
      1, // Page number on the document
      400, // X position
      240, // Y position
      80, // 100 pixels wide
      null)); // Default height
dfsle.Tab New_lastname_tab = new dfsle.LastNameTab()
      .withName('AdditionLastName')
      .withPosition(new dfsle.Tab.Position(
      1, // The document to use
      1, // Page number on the document
      500, // X position
      240, // Y position
      80, // 100 pixels wide
      null)); // Default height
dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
  myRecipient2.withTabs(new List<dfsle.Tab> {prefill_firstname,prefill_lastname,New_firstname_tab,New_lastname_tab});
  myRecipient2.withRole('Manager');  // remember to specify the role that match the Role in your template.
As for the envelope Custom Fields:
dfsle.CustomField myField = new dfsle.CustomField ('text', 'DSFSSourceObjectId', conStr, null, false, false);
Note: Envelope Custom Fields aren't directly visible to a recipient during a signing session, since you have set the show parameter to 'false', it will not be included in the Certificate of Completion. However, it should still be visible through web console reporting and API:
https://www.docusign.com/blog/dsdev-trenches-tabs-and-custom-fields

The file uploaded by the POST request is displayed as corrupted

const boundary = "foo_bar_baz";
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
let metadata = {
name: fileList[0].name,
mimeType: fileList[0].type,
parents: [response.data.id]
}
let body =
delimiter +
'Content-Type: application/json; charset=UTF-8\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + fileList[0].type + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +multipartReauestBody + close_delim;
console.log(body);
axios({
method: "post",
url: "https://www.googleapis.com/upload/drive/v3/files?supportsTeamDrives=true&uploadType=multipart",
headers: {
Authorization: "Bearer " + accessToken,
"Content-Type": "multipart/related; boundary=foo_bar_baz"
},
data: body
}).then(response => console.log(response)).catch(error => console.log(error))
The code above is to upload files to the shared drive using GOOGLE API.
const getBase64 = (file) => {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
console.log(multipartReauestBody= btoa(reader.result));
console.log(multipartReauestBody);
}
};
const uploadHandler = (files) => {
const filesLength = files.length;
for(let i = 0; i < filesLength; i++) {
const file = files[i];
fileList.push(file);
getBase64(file);
}
}
The upload handler works when selecting a file through the tag.
getBase64 converts the selected file into base64.
Everything seemed to work well as shown in the picture below. (A file has been created on the shared drive.)
However, when I click the generated file, I see an error that I cannot open the file.
And when I open the file in the notepad, it looks like the base64 code converted using getBase64.
Below is what appears when the original file is opened as a notepad.
As a result of checking the file size, the uploaded file was about 15,000 bytes larger than the original file.
How can I modify the code so that I can see the same content as the original file? Please help me.

Push an object into an object array then every property of object become undefined - Typescript

I tried to push an object into an object array as below (I want to use this way to update UI using Angular4):
In CampgroundService.ts
export class CampgroundDetail {
campground: Campground;
comments: Comment[];
}
In CampgroundDetailComponent.ts
#Component({
selector: 'campDetail',
templateUrl: './app/components/campgrounds/campground.detail.component.html',
styleUrls: ['./app/components/campgrounds/campgrounds.component.css']
})
export class CampgroundDetailComponent {
campDetail: CampgroundDetail = new CampgroundDetail();
updateUI(comment: Comment) {
console.log(comment);
this.campDetail.comments.push(
{
id: comment.id,
text: comment.text,
campground_id: comment.campground_id,
username: comment.username,
user_id: comment.user_id
});
console.log(this.campDetail.comments);
}
}
In campground.detail.component.html
<app-comment *ngIf="userdata" [comment]="selectedComment" (insertedComment)="updateUI($event)"></app-comment>
In CommentFormComponent.ts
#Component({
selector: 'app-comment',
templateUrl: './app/components/campgrounds/comment.form.component.html',
styleUrls: ['./app/components/campgrounds/campgrounds.component.css']
})
export class CommentFormComponent {
#Output() insertedComment = new EventEmitter<Comment>();
doSubmit() {
this.comment.user_id = this.userdata.id;
this.comment.username = this.userdata.username;
this.comment.campground_id = this.campground_id;
this.campgroundService.createComment(this.comment)
.then(data => {
this.campgroundService.getComment(data.comment_id)
.then(comment => this.insertedComment.emit(comment));
}).catch(error => {
if (error.status === 403) {
this.userService.flush();
this.router.navigate(['/login']);
}
});
}
}
According to the first console log, the comment object is not null.
However, after pushing into the array every property of object became undefined.
I tried to search on google as much as possible, but I still cannot find any solution. What am I missing? I will appreciate if anyone would give me any suggestion.
I found a solution finally. I modified CampgroundService.ts
export class CampgroundDetail {
campground: Campground;
comments: any[];
}
and modified CampgroundDetailComponent.ts
#Component({
selector: 'campDetail',
templateUrl: './app/components/campgrounds/campground.detail.component.html',
styleUrls: ['./app/components/campgrounds/campgrounds.component.css']
})
export class CampgroundDetailComponent {
campDetail: CampgroundDetail = new CampgroundDetail();
updateUI(comment: Comment) {
let tempComment = comment['comment'];
this.campDetail.comments.push(tempComment);
}
}
It works now.

How can I parse json array from json object?

I have a json object being returned from a get request in reactjs. I am wanting to get key and value items from this.
{"return":"Success",
"Items": [
{"Name":"centracore", "Type":"rollover" ,  "Os":"Windows",  "Level":"1", "Language_Used":"Assembly", "Size":"4mb"},
{"Name":"centracore", "Type":"Atype" ,   "Os":"Linux"  ,  "Level":"3", "Language_Used":"C++"     , "Size":"4mb"},
{"Name":"centracore", "Type":"random" ,  "Os":"OSX"    ,  "Level":"2", "Language_Used":"C"       , "Size":"4mb"}
]}
I noticed I can access the values manually using the code below.
Keep in mind, that this.state.stuff is holding my json object. Is my format json format bad for what I am trying to do?
if(this.state.authenticated ){
        {this.GetPage.bind(this)}
        let x = this.state.stuff; Object.entries(this.state.stuff).map((type,item) => {
        console.log(type[1][0])
        console.log(type[1][1])
        console.log(type[1][2])
      })
As far as I understand you want to be able to get the key and values within the Object Items arrays, so you need to map over items array and then the key, values from the obtained item with Object.entries()
You can do it like
if(this.state.authenticated ){
{this.GetPage.bind(this)}
this.state.stuff.Items.map((item, index) => {
Object.entries(item).forEach([key, value], () => {
console.log(key, value)
})
})
Working example
var obj = {"return":"Success",
"Items": [
{"Name":"centracore", "Type":"rollover" , "Os":"Windows", "Level":"1", "Language_Used":"Assembly", "Size":"4mb"},
{"Name":"centracore", "Type":"Atype" , "Os":"Linux" , "Level":"3", "Language_Used":"C++" , "Size":"4mb"},
{"Name":"centracore", "Type":"random" , "Os":"OSX" , "Level":"2", "Language_Used":"C" , "Size":"4mb"}
]}
obj.Items.map((item, index) => {
console.log( item);
Object.entries(item).forEach(([key, value]) => {
console.log(key, value)
})
})
JSFIDDLE

Can I sort children different than parent in CakePHP 3 threaded query?

I have a questions regarding threaded comments. Is it possible to order comments DESC and child comments ASC from this query (or table level) or should I make a after query modification?
Below you can find my query that orders all to DESC.
```
$comments = $this->Comments
        ->find('threaded', ['order' => ['Comments.created' => 'DESC']])
           ->contain(['Users'])
           ->matching(
               'BoardItems',
               function ($q) use ($boardItemId) {
                   return $q->where(
                       [
                           'BoardItems.id' => $boardItemId
                       ]
                   );
               }
           )
           ->all();
```
On SQL Level
You should be able to apply the solution suggested in MySql: ORDER BY parent and child, which
uses COALESCE to group/sort the parents first
groups the children by testing for a non-NULL parent ID
sorts the grouped children
In your case you'd sort by created instead of id, ie something like
ORDER BY
COALESCE(Comments.parent_id, Comments.created) DESC,
Comments.parent_id IS NOT NULL,
Comments.created ASC
To build this in a proper query builder-ish fashion, you'd have to use the order() and orderDesc() methods, so that you can use query expressions, something along the lines of
$query = $this->Comments
->find('threaded');
$comments = $query
// ->contain(...)
// ->matching(...)
// COALESCE(Comments.parent_id, Comments.created) DESC
->orderDesc($query->func()->coalesce([
'Comments.parent_id' => 'identifier',
'Comments.created' => 'identifier'
]))
// Comments.parent_id IS NOT NULL
->order($query->newExpr()->isNotNull('Comments.parent_id'))
// Comments.created ASC
->order(['Comments.created' => 'ASC'])
->all();
See also
Cookbook > Database Access & ORM > Query Builder > Selecting Data
Cookbook > Database Access & ORM > Query Builder > Using SQL Functions
Cookbook > Database Access & ORM > Query Builder > Advanced Conditions
On PHP level
Sorting things afterwards would be an option too, for example using a recursive result formatter:
$sortChildren = function($row) use (&$sortChildren) {
if (!empty($row['children'])) {
$row['children'] =
collection($row['children'])
->sortBy('created', SORT_ASC)
->map($sortChildren)
->toArray();
}
return $row;
};
$comments = $this->Comments
->find('threaded')
// ->contain(...)
// ->matching(...)
->order(['Comments.created' => 'DESC'])
->formatResults(function ($results) use ($sortChildren) {
return $results->map($sortChildren);
})
->all();
This would retrieve everything sort descending, and then sort all children arrays ascending by the created field. Similarily you could sort things before you output/use them in your views, depending on what exactly you're planning to do with the results.
If you want to keep things in the table, you could for example wrap this all up in a custom finder and/or retrieve the sort closure via a method on your table class.
See also
Cookbook > Collections > Sorting
Cookbook > Collections > Iterating

Resources