Remove files from Server End

Discussions about CSEntry
Post Reply
khurshid.arshad
Posts: 618
Joined: July 9th, 2012, 11:32 am
Location: Islamabad, Pakistan

Remove files from Server End

Post by khurshid.arshad »

Dear CSPro team;

I want to remove file from server end after getting on tablet. Example

Code: Select all

syncfile (get, "/Map/9990001.kml","..\..\KML\");
After get 9990001.kml the file from server, want to delete through application.

Best regards.
a.
Gregory Martin
Posts: 1948
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Remove files from Server End

Post by Gregory Martin »

With the current sync functions, there is no way to delete files on the server. That is something that we can think about for a future release. What is the reason that you need to delete the file from the server?
khurshid.arshad
Posts: 618
Joined: July 9th, 2012, 11:32 am
Location: Islamabad, Pakistan

Re: Remove files from Server End

Post by khurshid.arshad »

Dear Gregory;

I hope you are doing well.

We have thousands of KML files in my deploy application. Due to internet connection issue, it is difficult to install this application in the field.

Due to this issue we put all these Maps in "Wamp64/www/csweb/files" folder.

Our team allocation is on tehsil level and each team has 10 Enumerators. Every team (10 Enumerators) has its own EA list in their tablets.

In case an enumerator download the KML in the respective tehsil the other 9 enumerators should not be allowed to download this KML.

That is why we want to remove this KML from the server.

Best regards.
a.
Gregory Martin
Posts: 1948
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Remove files from Server End

Post by Gregory Martin »

I think the ideal approach would be to have a dictionary that keeps track of what enumerators have downloaded what files. That way, you can use syncdata to update this information and then only get files related to assignments (maps) that have not been taken by someone else.

If you want to just work with files, you could upload files to indicate that a file has been downloaded. So, if someone downloads 9990001.kml, you could upload a blank file, say, 9990001.kml.download, to indicate that it's been downloaded. Then, before another enumerator downloads a map, you use try to get XXX.kml.download, and only if it fails do you allow the enumerator to download the map.
khurshid.arshad
Posts: 618
Joined: July 9th, 2012, 11:32 am
Location: Islamabad, Pakistan

Re: Remove files from Server End

Post by khurshid.arshad »

Dear Gregory;

Actually I am following your second approach. When the Enumerator downloads the 9990001.KML file from the server, at the same time he left a 9990001.txt file on the server. This file will be create at runtime. This text file contain KML file name enumerator code and word Activate.

On the other hand if this enumerator declines 9990001.KML due to some reason then the application will get the file from the server and add another line with KML file name, enumerator code and word Deactivated.

Now if someone else wants to work on 9990001.KML then the application will get 9990001.txt file first and check whether the KML is activated or deactivated. If it is deactivated then the other enumerator can download kml otherwise it can not.

At this stage i am using a separate file for the deactivation information but later i will use a single text file to store the information.

I am sharing my code just for reference.

Best regards.
a.

Code: Select all

Function ReceiveMapfromServer();

	if direxist("..\..\KML\") then
	else
		dircreate("..\..\KML\");
	endif;

	
	if fileexist("..\..\KML\"+ key (SAMPLE_DICT) [1:7]+".kml") then
		errmsg ("You have already download this file.");
		move to PRO_SUP_ID;

	elseif 	syncconnect(CSWeb, "http://203.82.55.214/csweb75/api") = 1 then
		//First check if there is Dactivation informaation against this EA
		syncfile (get, "/Maptxt/"+ "Dact-"+key (SAMPLE_DICT) [1:7]+".txt","..\..\KML\");//First check Dactivated EA
			if fileexist("..\..\KML\"+"Dact-"+key (SAMPLE_DICT) [1:7]+".txt") then
					setfile (FileKMLTxt, "..\..\KML\"+"Dact-"+key (SAMPLE_DICT) [1:7]+".txt");
					
					while fileread (FileKMLTxt, PreLoad_Data) do;
					string ActiveDactive =PreLoad_Data[33:11];

					enddo;
		else
		//If deactive file does not exist on the server
		syncfile (get, "/Maptxt/"+ key (SAMPLE_DICT) [1:7]+".txt","..\..\KML\");
			//If file receive from the server
		if fileexist("..\..\KML\"+key (SAMPLE_DICT) [1:7]+".txt") then
			//If EA already get someone else
			message1="سلیکٹ کیے  گے اینومریشن ایریا نمبر ";
			message2="میں آپ کام نہیں کر سکتے";

			set errmsg (operator);
			errmsg ("%s\n\n%s\n\n%s", message1,key (SAMPLE_DICT) [1:7],message2);
			set errmsg (default);
			syncdisconnect();
			filedelete("..\..\KML\"+key (SAMPLE_DICT) [1:7]+".txt");
			move to PRO_SUP_ID;
		else
			syncfile (get, "/Map/"+ key (SAMPLE_DICT) [1:7]+".kml","..\..\KML\");
			if fileexist("..\..\KML\"+key (SAMPLE_DICT) [1:7]+".kml")=0 then
			//If map does not exist on the server
				message1="سلیکٹ کیے  گے اینومریشن ایریا نمبر ";
				message2="کا نقشہ ابھی موجود نہیں ہے۔ ";
				message3= "اس اینومریشن ایریا کا نقشہ آپ کو بعد میں دیا جاے گا";
					set errmsg (operator);
					errmsg ("%s\n\n[%04s]\n\n%s\n\n%s.", message1, key (SAMPLE_DICT) [1:7],message2,message3);
					set errmsg (default);

				move to PRO_SUP_ID;
			else
				//Write text file 
				setfile (FileKMLTxt, "..\..\KML\"+key (SAMPLE_DICT) [1:7]+".txt",create);
				filewrite(FileKMLTxt, key (SAMPLE_DICT) [1:7]+"-"+sysparm()[1:5]);
				close(FileKMLTxt);
				syncfile (PUT,"..\..\KML\"+key (SAMPLE_DICT) [1:7]+".txt", "/Maptxt/");
				syncdisconnect();
				syncfile (get, "/Map/"+ key (SAMPLE_DICT) [1:7]+".kml","..\..\KML\");
				errmsg ("Map for EA No. [%04s] has been downloaded.", key (SAMPLE_DICT) [1:7]);
				move to PRO_SUP_ID;

			endif;
		endif

		endif;


	else
				//if internet not working
				message1="آپ یہ ایکٹیوٹی اس ٹیبلیٹ پر نہیں کر سکتے کیوں کہ آپ کے ٹیبلیٹ پر اس وقت ";
				message2="انٹرنیٹ ";
				message3="موجود نہیں ہے۔";
					set errmsg (operator);
					if errmsg(1, message1,message2,message3) then
						set errmsg (default);
						move to Pro_Sup_ID;
					else
					move to Pro_Sup_ID;
					endif;
	
	endif;

end;


Function DeclineEA()

	if fileexist("..\..\KML\"+key (SAMPLE_DICT) [1:7]+".kml") then
			//If decline the EA
			message1="Decline Enumeration Area:";
			message2="کا مطلب ہے";
			message3="کہ آپ   اس ایریا  نمبر ";
			message4="کا تقشہ سرور سے ڈوان لوڈ کر چکے ہیں";
			message5="اور اب اس میں کام نہیں کرنا چاہتے   اور نہ ہی آپ نے اس ایریا میں کام شروع کیا۔ تو اس صورت میں اس ایریا کا نقشہ آپ کے ٹیبلیٹ سے ختم ہو جاے گا۔";
			set errmsg (operator);
			errmsg ("%s\n%s\n\n%s\n\n%s\n\n%s\n\n%s", message1,message2,message3,key (SAMPLE_DICT) [1:7], message4, message5);
			set errmsg (default);


						Index=accept ("Decline Enumeration Area",
											"نہیں، غلط آپشن کا انتخاب ہو گیا ہے",
											"                                               ",
											"کیا آپ اس ایریا کا نقشہ دیکلاین کرنا چاہتے ہیں");
			if index =1 then
				move to Pro_sup_ID;

			elseif index =2 then
				errmsg("You did not select any option.");
				move to Pro_sup_ID;
			elseif index=3 then
				if 	syncconnect(CSWeb, "http://203.82.55.214/csweb75/api") = 0 then
						message1="آپ یہ ایکٹیوٹی اس ٹیبلیٹ پر نہیں کر سکتے کیوں کہ آپ کے ٹیبلیٹ پر اس وقت ";
						message2="انٹرنیٹ ";
						message3="موجود نہیں ہے۔";
						set errmsg (operator);
					if errmsg(1, message1,message2,message3) then
						set errmsg (default);
						move to Pro_Sup_ID;
					else
						move to Pro_Sup_ID;
					endif;
				else
				setfile (FileKMLTxt, "..\..\KML\"+"Dact-"+key (SAMPLE_DICT) [1:7]+".txt",create);
				filewrite(FileKMLTxt, "Dact-"+key (SAMPLE_DICT) [1:7]+"-"+sysparm()[1:5]+ " File has been Deactivated");
				close(FileKMLTxt);
				syncfile (PUT,"..\..\KML\"+"Dact-"+key (SAMPLE_DICT) [1:7]+".txt", "/Maptxt/");
				syncdisconnect();


					filedelete("..\..\KML\"+ key (SAMPLE_DICT) [1:7]+".KML");
					filedelete("..\..\KML\"+ key (SAMPLE_DICT) [1:7]+".txt");
					filedelete("..\..\KML\"+ "Dact-"+key (SAMPLE_DICT) [1:7]+".txt");

					set errmsg (operator);
						errmsg ("The Enumeration Area No\n\n..%s..\n\nhas been Deactivated.", key (SAMPLE_DICT) [1:7]);
					set errmsg (default);
						move to Pro_sup_ID;
				endif;
			endif
	else
		set errmsg (operator);
			errmsg ("You don't have Enumeration Area No\n\n..%s..\n\nfor Deactivation.", key (SAMPLE_DICT) [1:7]);
		set errmsg (default);
		Move to Pro_Sup_ID;
	endif;
end;
Gregory Martin
Posts: 1948
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Remove files from Server End

Post by Gregory Martin »

Arshad, one trick you might find neat is that in CSPro if you highlight code and say Ctrl+Shift+F (F= CSPro Users Forum) it will copy the code in a way that you can paste here and get the syntax coloring. That definitely would make it easier for people to review the code that you helpfully shared.
khurshid.arshad
Posts: 618
Joined: July 9th, 2012, 11:32 am
Location: Islamabad, Pakistan

Re: Remove files from Server End

Post by khurshid.arshad »

Dear Gregory;

Thank you and here is the code for your review.

Best regards.
a.
Function gettxtkmlfromserver();

    //First get Text file form the server of the respective KML file
   
syncfile (get, "/Maptxt/"+key (SAMPLE_DICT) [1:7]+".txt","..\109_KML\");

            //If Text file exist on the server end and successfully downloaded
       
if fileexist("..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt") then
                setfile
(FileKMLTxt, "..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt");
               
while fileread (FileKMLTxt, PreLoad_Data) do;
                    ActiveDactive =PreLoad_Data[
15:13];
               
enddo;
               
close(FileKMLTxt);
               
if strip(ActiveDactive)="Activated" then
                    //MessageTranslation: KML is Activated, so you can not start wrok in Selected EA.
                   
message1="سلیکٹ کیے  گے اینومریشن ایریا نمبر ";
                    message2=
"میں آپ کام نہیں کر سکتے";
                   
set errmsg (operator);
                   
errmsg ("%s\n\n%s\n\n%s", message1,key (SAMPLE_DICT) [1:7],message2);
                   
set errmsg (default);
                   
syncdisconnect();
                   
filedelete("..\109_KML\"+ key (SAMPLE_DICT) [1:7]+".txt");
                   
move to PRO_SUP_ID;

               
else
                    //MessageTranslation: KML is Deactivated, then convert as Activated and get KML.
                   
syncfile (get, "/Map/"+ key (SAMPLE_DICT) [1:7]+".kml","..\109_KML\");

                   
setfile (FileKMLTxt, "..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt");
                    FileKMLTxt.open(
"..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt", append);
                   
filewrite(FileKMLTxt, key (SAMPLE_DICT) [1:7]+"-"+sysparm()[1:5]+"-Activated");
                   
close(FileKMLTxt);

               
syncfile (PUT,"..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt", "/Maptxt/");
                   
syncdisconnect();
               
move to PRO_SUP_ID;


               
endif;
       
else
                {1: If Text file does not exist on the server it means no one Downloaded the KML yet.
                2: KML is going to download first time
                3: First check the KML exist on the server end or not}
               
syncfile (get, "/Map/"+ key (SAMPLE_DICT) [1:7]+".kml","..\109_KML\");

                {If KML does not exist on the server end and not downloaded.}
               
if fileexist("..\109_KML\"+key (SAMPLE_DICT) [1:7]+".kml")=0 then
                    {Message Translation: You don't have Map, so you can not start work now}
                   
message1="سلیکٹ کیے  گے اینومریشن ایریا نمبر ";
                    message2=
"کا نقشہ ابھی موجود نہیں ہے۔ ";
                    message3=
"اس اینومریشن ایریا کا نقشہ آپ کو بعد میں دیا جاے گا";
                       
set errmsg (operator);
                       
errmsg ("%s\n\n[%04s]\n\n%s\n\n%s.", message1, key (SAMPLE_DICT) [1:7],message2,message3);
                       
set errmsg (default);
                       
move to PRO_SUP_ID;
               
else
                    {If KML exist on the server and Downloading first time then Text file will not be exist on the server end.
                     It will be create First Time after downloading the KML from the server}
                   
syncfile (get, "/Map/"+ key (SAMPLE_DICT) [1:7]+".kml","..\109_KML\");
                        {After successfully downloading the KML file create text file with following information:
                         KML file name, Enumerator Code and word Activate}
                       
setfile (FileKMLTxt, "..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt",create);
                           
filewrite(FileKMLTxt, key (SAMPLE_DICT) [1:7]+"-"+sysparm()[1:5]+"-Activated");
                           
close(FileKMLTxt);

                       
syncfile (PUT,"..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt", "/Maptxt/");
                       
syncdisconnect();

                       
errmsg ("Map for EA No. [%04s] has been downloaded.", key (SAMPLE_DICT) [1:7]);
                       
move to PRO_SUP_ID;



               
endif;

       
endif;
end;


Function ReceiveMapfromServer();



    //First check already downloaded the KML file or Not
   
if fileexist("..\109_KML\"+ key (SAMPLE_DICT) [1:7]+".kml") then
        set errmsg
(operator);
       
errmsg ("You have already downloaded\n\n%s.kml.",key (SAMPLE_DICT) [1:7]);
       
set errmsg (default);
       
move to PRO_SUP_ID;

        //If KML file not downloaded then first check Internet connection
   
elseif  syncconnect(CSWeb, "http://localhost/csweb75/api") = 1 then
        {1: If Internet connection is fine then download text file where we can find the status of requested KML;
         2: If text file does not exist on the server, it means the KML file available for working;
         3: Download the KML from the server;
         4: Create a txt file and add a line with KML file name, enumerator code and word Activated;
         5: PUT text file on the server for others;
         6: If text file available then we will see the KML file is active or Deactive;
            6.1: Active mean: some one is already working on it and you will not allow to work on it.
            6.2: Deactive mean: Enumerator get the KML and after that enumerator declines KML due to some reason then the application
                 will get the text file from the server and add another line with KML file name, enumerator code and word Deactivated and
                 PUT on the server for others.
        }
           
gettxtkmlfromserver();

   
else
        //if internet not working
                    //Message Translation: You don't have internet access right now on your tablet
                   
message1="آپ یہ ایکٹیوٹی اس ٹیبلیٹ پر نہیں کر سکتے کیوں کہ آپ کے ٹیبلیٹ پر اس وقت ";
                    message2=
"انٹرنیٹ ";
                    message3=
"موجود نہیں ہے۔";
                       
set errmsg (operator);
        //errmsg(1, message1,message2,message3) is comming from my mgf file, which is avabilabe as external file in my app
       
if errmsg(1, message1,message2,message3) then
            set errmsg
(default);
           
move to Pro_Sup_ID;
       
else
            move to
Pro_Sup_ID;
       
endif;

   
endif;

end;


Function DeclineEA()

    //First check KML file exist on the enumerator tablet or not
   
if fileexist("..\109_KML\"+key (SAMPLE_DICT) [1:7]+".kml") then
        {1: If declining the KML
        2: Message Translation: Decline KML mean you don't want to start work on this Area,
           which you have already download the KML}
       
message1="Decline KML:";
        message2=
"کا مطلب ہے";
        message3=
"کہ آپ   اس ایریا  نمبر ";
        message4=
"کا تقشہ سرور سے ڈوان لوڈ کر چکے ہیں";
        message5=
"اور اب اس میں کام نہیں کرنا چاہتے   اور نہ ہی آپ نے اس ایریا میں کام شروع کیا۔ تو اس صورت میں اس ایریا کا نقشہ آپ کے ٹیبلیٹ سے ختم ہو جاے گا۔";
       
set errmsg (operator);
       
errmsg ("%s\n%s\n\n%s\n\n%s\n\n%s\n\n%s", message1,message2,message3,key (SAMPLE_DICT) [1:7], message4, message5);
       
set errmsg (default);

                    Index=
accept ("Decline Enumeration Area",
                                       
"نہیں، غلط آپشن کا انتخاب ہو گیا ہے",                  //No, Wrong selection
                                       
"                                               ",
                                       
"کیا آپ اس ایریا کا نقشہ دیکلاین کرنا چاہتے ہیں");         //Yes, I want to decline EA
       
if index =1 then
            move to
Pro_sup_ID;

       
elseif index =2 then
            errmsg
("You did not select any option.");
           
move to Pro_sup_ID;
       
elseif index=3 then

            //Check Internet Connection
           
if  syncconnect(CSWeb, "http://localhost/csweb75/api") = 0 then
                {1: If inernet connection not working
                 2: Message Translation: You can not perform this activity, because you don't have internet access}
               
message1="آپ یہ ایکٹیوٹی اس ٹیبلیٹ پر نہیں کر سکتے کیوں کہ آپ کے ٹیبلیٹ پر اس وقت ";
                message2=
"انٹرنیٹ ";
                message3=
"موجود نہیں ہے۔";
               
set errmsg (operator);

                //errmsg(1, message1,message2,message3) is comming from my mgf file, which is avabilabe as external file in my app
               
if errmsg(1, message1,message2,message3) then
                    set errmsg
(default);
                   
move to Pro_Sup_ID;
               
else
                    move to
Pro_Sup_ID;
               
endif;
           
else

                setfile
(FileKMLTxt, "..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt");
                FileKMLTxt.open(
"..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt", append);
               
filewrite(FileKMLTxt, key (SAMPLE_DICT) [1:7]+"-"+sysparm()[1:5]+"-Deactivated");
               
close(FileKMLTxt);
               
syncfile (PUT,"..\109_KML\"+key (SAMPLE_DICT) [1:7]+".txt", "/Maptxt/");
               
syncdisconnect();


                   
filedelete("..\109_KML\"+ key (SAMPLE_DICT) [1:7]+".KML");
                   
filedelete("..\109_KML\"+ key (SAMPLE_DICT) [1:7]+".txt");
                   
set errmsg (operator);
                       
errmsg ("The Enumeration Area No\n\n..%s..\n\nhas been Deactivated.", key (SAMPLE_DICT) [1:7]);
                   
set errmsg (default);
                       
move to Pro_sup_ID;
           
endif;
       
endif
    else
    //If KML file does not exist on the enumerator tablet
       
set errmsg (operator);
           
errmsg ("You don't have Enumeration Area No\n\n..%s..\n\nfor Deactivation.", key (SAMPLE_DICT) [1:7]);
       
set errmsg (default);
       
Move to Pro_Sup_ID;
   
endif;
end;
Post Reply