Browse code

[cli] BM-14592 Feat : calendar and addressbook export now export all their calendars/addressbooks by default

Arnaud Aujon Chevallier authored on 10/04/2019 13:21:18
Showing 3 changed files
... ...
@@ -18,12 +18,15 @@
18 18
 package net.bluemind.cli.calendar;
19 19
 
20 20
 import java.io.File;
21
+import java.util.ArrayList;
22
+import java.util.List;
21 23
 import java.util.Optional;
22 24
 
23 25
 import io.airlift.airline.Arguments;
24 26
 import io.airlift.airline.Command;
25 27
 import io.airlift.airline.Option;
26 28
 import net.bluemind.calendar.api.CalendarContainerType;
29
+import net.bluemind.calendar.api.CalendarDescriptor;
27 30
 import net.bluemind.calendar.api.IVEvent;
28 31
 import net.bluemind.cli.cmd.api.CliContext;
29 32
 import net.bluemind.cli.cmd.api.CliException;
... ...
@@ -31,6 +34,9 @@ import net.bluemind.cli.cmd.api.ICmdLet;
31 34
 import net.bluemind.cli.cmd.api.ICmdLetRegistration;
32 35
 import net.bluemind.cli.utils.CliUtils;
33 36
 import net.bluemind.core.api.Regex;
37
+import net.bluemind.core.container.api.ContainerQuery;
38
+import net.bluemind.core.container.api.IContainers;
39
+import net.bluemind.core.container.model.ContainerDescriptor;
34 40
 import net.bluemind.core.rest.base.GenericStream;
35 41
 
36 42
 @Command(name = "export", description = "Export a calendar to an ICS file")
... ...
@@ -52,10 +58,10 @@ public class ExportCalendarCommand implements ICmdLet, Runnable {
52 58
 	@Arguments(required = true, description = "email address")
53 59
 	public String email;
54 60
 
55
-	@Option(name = "--ics-file-path", description = "The ics file path")
56
-	public String icsFilePath;
61
+	@Option(name = "--output-directory", description = "The output directory path, files will be save in an email named subdirectory, default is /tmp")
62
+	public String outputDirectory = "/tmp";
57 63
 
58
-	@Option(name = "--calendarUid", description = "calendar uid, default value is user default calendar")
64
+	@Option(name = "--calendarUid", description = "calendar uid, export all calendars if not specified")
59 65
 	public String calendarUid;
60 66
 
61 67
 	@Option(name = "--dry", description = "Dry-run (do nothing)")
... ...
@@ -78,26 +84,36 @@ public class ExportCalendarCommand implements ICmdLet, Runnable {
78 84
 		}
79 85
 
80 86
 		String userUid = cliUtils.getUserUidFromEmail(email);
81
-
82
-		if (icsFilePath == null) {
83
-			icsFilePath = "/tmp/calendar-" + email + ".ics";
84
-		}
85
-		File file = new File(icsFilePath);
86
-		if (file.exists() || file.isDirectory()) {
87
-			throw new CliException("File " + icsFilePath + " already exist.");
87
+		String domainUid = cliUtils.getDomainUidFromEmail(email);
88
+		
89
+		outputDirectory += "/" + email;
90
+		
91
+		File directory = new File(outputDirectory);
92
+		if (!directory.exists()) {
93
+			directory.mkdirs();
88 94
 		}
89 95
 
90
-		String calendarUid = null;
96
+		List<ContainerDescriptor> calendars = new ArrayList<>();
97
+		IContainers containersService = ctx.adminApi().instance(IContainers.class);
98
+		ContainerQuery q = ContainerQuery.ownerAndType(userUid, CalendarContainerType.TYPE);
91 99
 		if (calendarUid == null) {
92
-			calendarUid = CalendarContainerType.defaultUserCalendar(userUid);
100
+			containersService.allForUser(domainUid, userUid, q).forEach(c -> calendars.add(c));
101
+		} else {
102
+			calendars.add(containersService.get(calendarUid));
93 103
 		}
94 104
 
95
-		// TODO manque un check calendarUid != null
96 105
 		if (!dry) {
97
-			GenericStream.streamToFile(ctx.adminApi().instance(IVEvent.class, calendarUid).exportAll(), file);
98
-			System.out.println("calendar " + calendarUid + " of " + email + " was exported");
99
-		} else {
100
-			System.out.println("DRY : calendar " + calendarUid + " of " + email + " was exported");
106
+			for (ContainerDescriptor cal : calendars) {
107
+				String filename = outputDirectory + "/" + cliUtils.encodeFilename(cal.name) + ".ics";
108
+				if (!dry) {
109
+					File icsFile = new File(filename);
110
+					if (icsFile.exists()) {
111
+						icsFile.delete();
112
+					}
113
+					GenericStream.streamToFile(ctx.adminApi().instance(IVEvent.class, cal.uid).exportAll(), icsFile);
114
+				}
115
+				ctx.info(dry? "DRY : " : "" + "calendar " + cal.uid + " of " + email + " was exported to " + filename);
116
+			}
101 117
 		}
102 118
 	}
103 119
 }
... ...
@@ -20,6 +20,8 @@ package net.bluemind.cli.contact;
20 20
 import java.io.File;
21 21
 import java.io.IOException;
22 22
 import java.nio.file.Files;
23
+import java.util.ArrayList;
24
+import java.util.List;
23 25
 import java.util.Optional;
24 26
 
25 27
 import io.airlift.airline.Arguments;
... ...
@@ -33,6 +35,9 @@ import net.bluemind.cli.cmd.api.ICmdLet;
33 35
 import net.bluemind.cli.cmd.api.ICmdLetRegistration;
34 36
 import net.bluemind.cli.utils.CliUtils;
35 37
 import net.bluemind.core.api.Regex;
38
+import net.bluemind.core.container.api.ContainerQuery;
39
+import net.bluemind.core.container.api.IContainers;
40
+import net.bluemind.core.container.model.ContainerDescriptor;
36 41
 
37 42
 @Command(name = "export", description = "Export an addressbook to an VCF file")
38 43
 public class ExportAddressBookCommand implements ICmdLet, Runnable {
... ...
@@ -53,10 +58,10 @@ public class ExportAddressBookCommand implements ICmdLet, Runnable {
53 58
 	@Arguments(required = true, description = "email address")
54 59
 	public String email;
55 60
 
56
-	@Option(name = "--vcf-file-path", description = "The vcf file path")
57
-	public String vcfFilePath;
61
+	@Option(name = "--output-directory", description = "The output directory path, files will be save in an email named subdirectory, default is /tmp")
62
+	public String outputDirectory = "/tmp";
58 63
 
59
-	@Option(name = "--addressbook-uid", description = "the addressbook uid. Default value: default addressbook")
64
+	@Option(name = "--addressbook-uid", description = "the addressbook uid. , export all addressbooks if not specified")
60 65
 	public String addressBookUid;
61 66
 
62 67
 	@Option(name = "--dry", description = "Dry-run (do nothing)")
... ...
@@ -79,33 +84,39 @@ public class ExportAddressBookCommand implements ICmdLet, Runnable {
79 84
 		}
80 85
 		
81 86
 		String userUid = cliUtils.getUserUidFromEmail(email);
87
+		String domainUid = cliUtils.getDomainUidFromEmail(email);
82 88
 
83
-		if (vcfFilePath == null) {
84
-			vcfFilePath = "/tmp/addressbook-" + email + ".vcf";
85
-		}
86
-		File file = new File(vcfFilePath);
87
-		if (file.exists() || file.isDirectory()) {
88
-			throw new CliException("File " + vcfFilePath + " already exist.");
89
+		outputDirectory += "/" + email;
90
+		
91
+		File directory = new File(outputDirectory);
92
+		if (!directory.exists()) {
93
+			directory.mkdirs();
89 94
 		}
90 95
 
96
+		List<ContainerDescriptor> addressbooks = new ArrayList<>();
97
+		IContainers containersService = ctx.adminApi().instance(IContainers.class);
98
+		ContainerQuery q = ContainerQuery.ownerAndType(userUid, AddressBookContainerType.TYPE);
91 99
 		if (addressBookUid == null) {
92
-			addressBookUid = AddressBookContainerType.defaultUserAddressbook(userUid);
100
+			containersService.allForUser(domainUid, userUid, q).forEach(c -> addressbooks.add(c));
101
+		} else {
102
+			addressbooks.add(containersService.get(addressBookUid));
93 103
 		}
94
-		
95
-		try {
104
+
105
+		for (ContainerDescriptor addressbook : addressbooks) {
106
+			String filename = outputDirectory + "/" + cliUtils.encodeFilename(addressbook.name) + ".vcf";
96 107
 			if (!dry) {
97
-				String exportedCards = ctx.adminApi().instance(IVCardService.class, addressBookUid).exportAll();
98
-				if (exportedCards == null) {
99
-					// empty addressbook
108
+				String exportedCards = ctx.adminApi().instance(IVCardService.class, addressbook.uid).exportAll();
109
+	 			if (exportedCards == null) {
100 110
 					exportedCards = "";
111
+	 			}
112
+	 			try {
113
+	 				File vcardFile = new File(filename);
114
+					Files.write(vcardFile.toPath(), exportedCards.getBytes());
115
+				} catch (IOException e) {
116
+					throw new CliException(e);
101 117
 				}
102
-				Files.write(file.toPath(), exportedCards.getBytes());
103
-				System.out.println("addressbook " + addressBookUid + " of " + email + " was exported");
104
-			} else {
105
-				System.out.println("DRY : addressbook " + addressBookUid + " of " + email + " was exported");
106 118
 			}
107
-		} catch(IOException e) {
108
-			throw new CliException(e.toString());
119
+			ctx.info(dry? "DRY : " : "" + "addressbook " + addressbook.uid + " of " + email + " was exported to " + filename);
109 120
 		}
110 121
 	}
111 122
 }
... ...
@@ -1,5 +1,6 @@
1 1
 package net.bluemind.cli.utils;
2 2
 
3
+import java.io.UnsupportedEncodingException;
3 4
 import java.util.Arrays;
4 5
 
5 6
 import net.bluemind.cli.cmd.api.CliContext;
... ...
@@ -57,4 +58,12 @@ public class CliUtils {
57 58
 		}
58 59
 		return result.values.get(0).uid;
59 60
 	}
61
+	
62
+	public String encodeFilename(String name) {
63
+		try {
64
+			return java.net.URLEncoder.encode(name, "UTF-8");
65
+		} catch (UnsupportedEncodingException e) {
66
+			throw new CliException("Encoding error : " + e.getMessage());	
67
+		}
68
+	}
60 69
 }