-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoriesManagement.java
More file actions
267 lines (233 loc) · 13.7 KB
/
Copy pathMemoriesManagement.java
File metadata and controls
267 lines (233 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import com.translated.lara.Credentials;
import com.translated.lara.errors.LaraException;
import com.translated.lara.translator.*;
import java.io.File;
import java.util.Arrays;
import java.util.List;
/**
* Complete memory management examples for the Lara Java SDK
*
* This example demonstrates:
* - Create, list, update, delete memories
* - Add individual translations
* - Multiple memory operations
* - TMX file import with progress monitoring
* - TMX import with callback URL (async notification)
* - Async memory export with callback URL
* - Translation deletion
* - Translation with TUID and context
* - Sharing a memory with the account or a group (add, rename, list, revoke)
*/
public class MemoriesManagement {
public static void main(String[] args) {
// Set your credentials here
String accessKeyId = "your-access-key-id";
String accessKeySecret = "your-access-key-secret";
Credentials credentials = new Credentials(accessKeyId, accessKeySecret);
Translator lara = new Translator(credentials);
String memoryId = null;
String externalMemoryId = null;
String memory2ToDelete = null;
try {
// Example 1: Basic memory management
System.out.println("=== Basic Memory Management ===");
Memory memory = lara.memories.create("MyDemoMemory");
System.out.println("✅ Created memory: " + memory.getName() + " (ID: " + memory.getId() + ")");
memoryId = memory.getId();
// Get memory details
Memory retrievedMemory = lara.memories.get(memoryId);
if (retrievedMemory != null) {
System.out.println("📖 Memory: " + retrievedMemory.getName() + " (Owner: " + retrievedMemory.getOwnerId() + ")");
}
// Update memory
Memory updatedMemory = lara.memories.update(memoryId, "UpdatedDemoMemory");
System.out.println("📝 Updated name: '" + memory.getName() + "' -> '" + updatedMemory.getName() + "'");
System.out.println();
// List all memories
List<Memory> memories = lara.memories.list();
System.out.println("📝 Total memories: " + memories.size());
System.out.println();
// Example 2: Adding translations
// Important: To update/overwrite a translation unit you must provide a tuid. Calls without a tuid always create a new unit and will not update existing entries.
System.out.println("=== Adding Translations ===");
try {
// Basic translation addition (with TUID)
MemoryImport memImport1 = lara.memories.addTranslation(memoryId, "en-US", "fr-FR", "Hello", "Bonjour", "greeting_001");
System.out.println("✅ Added: 'Hello' -> 'Bonjour' with TUID 'greeting_001' (Import ID: " + memImport1.getId() + ")");
// Translation with context
MemoryImport memImport2 = lara.memories.addTranslation(
memoryId, "en-US", "fr-FR", "How are you?", "Comment allez-vous?", "greeting_002",
"Good morning", "Have a nice day"
);
System.out.println("✅ Added with context (Import ID: " + memImport2.getId() + ")");
System.out.println();
} catch (LaraException e) {
System.out.println("Error adding translations: " + e.getMessage() + "\n");
}
// Example 3: Multiple memory operations
System.out.println("=== Multiple Memory Operations ===");
try {
// Create second memory for multi-memory operations
Memory memory2 = lara.memories.create("SecondDemoMemory");
String memory2Id = memory2.getId();
System.out.println("✅ Created second memory: " + memory2.getName());
// Add translation to multiple memories (with TUID)
List<String> memoryIds = Arrays.asList(memoryId, memory2Id);
MemoryImport multiImportJob = lara.memories.addTranslation(memoryIds, "en-US", "it-IT", "Hello World!", "Ciao Mondo!", "greeting_003");
System.out.println("✅ Added translation to multiple memories (Import ID: " + multiImportJob.getId() + ")");
System.out.println();
// Store for cleanup
memory2ToDelete = memory2Id;
} catch (LaraException e) {
System.out.println("Error with multiple memory operations: " + e.getMessage() + "\n");
memory2ToDelete = null;
}
// Example 4: TMX import functionality
System.out.println("=== TMX Import Functionality ===");
// Replace with your actual TMX file path
String tmxFilePath = "sample_memory.tmx"; // Create this file with your TMX content
File tmxFile = new File(tmxFilePath);
if (tmxFile.exists()) {
try {
System.out.println("Importing TMX file: " + tmxFile.getName());
MemoryImport tmxImport = lara.memories.importTmx(memoryId, tmxFile);
System.out.println("Import started with ID: " + tmxImport.getId());
System.out.println("Initial progress: " + (tmxImport.getProgress() * 100) + "%");
// Wait for import to complete
try {
MemoryImport completedImport = lara.memories.waitForImport(tmxImport, 10000L); // 10 seconds timeout
System.out.println("✅ Import completed!");
System.out.println("Final progress: " + (completedImport.getProgress() * 100) + "%");
} catch (InterruptedException e) {
System.out.println("Import timeout: The import process took too long to complete.");
}
System.out.println();
} catch (LaraException e) {
System.out.println("Error with TMX import: " + e.getMessage() + "\n");
}
} else {
System.out.println("TMX file not found: " + tmxFilePath);
}
// Example 5: Async TMX import with callback URL
System.out.println("=== Async TMX Import with Callback URL ===");
if (tmxFile.exists()) {
try {
String importCallbackUrl = "https://your-server.example.com/callbacks/memory-import";
MemoryImport asyncImport = lara.memories.importTmx(memoryId, tmxFile, importCallbackUrl);
System.out.println("✅ Async import started (ID: " + asyncImport.getId() + ")");
System.out.println(" Callback will be sent to: " + importCallbackUrl);
System.out.println();
} catch (LaraException e) {
System.out.println("Error with async TMX import: " + e.getMessage() + "\n");
}
} else {
System.out.println("TMX file not found, skipping async import example.\n");
}
// Example 6: Async memory export
System.out.println("=== Async Memory Export ===");
try {
String exportCallbackUrl = "https://your-server.example.com/callbacks/memory-export";
// Export with default format
MemoryExport exportJob = lara.memories.exportAsync(memoryId, exportCallbackUrl);
System.out.println("✅ Export triggered (job_id: " + exportJob.getJobId() + ")");
System.out.println(" Callback will be sent to: " + exportCallbackUrl);
// Export with specific format
MemoryExport exportTmxJob = lara.memories.exportAsync(memoryId, exportCallbackUrl, Memory.ExportFormat.TMX);
System.out.println("✅ TMX export triggered (job_id: " + exportTmxJob.getJobId() + ")");
System.out.println();
} catch (LaraException e) {
System.out.println("Error with async export: " + e.getMessage() + "\n");
}
// Example 7: Translation deletion
System.out.println("=== Translation Deletion ===");
try {
// Delete a specific translation unit (with TUID)
// Important: if you omit tuid, all entries that match the provided fields will be removed
MemoryImport deleteJob = lara.memories.deleteTranslation(memoryId,
"en-US",
"fr-FR",
"Hello",
"Bonjour",
tuid="greeting_001" // Specify the TUID to delete a specific translation unit
);
System.out.println("🗑️ Deleted translation unit (Job ID: " + deleteJob.getId() + ")");
System.out.println();
} catch (LaraException e) {
System.out.println("Error deleting translation: " + e.getMessage() + "\n");
}
// Example 8: Memory sharing
// Sharing requires a multi-user account and the appropriate role (account owner for
// account-wide shares, owner/admin for group shares). Each call returns the shared
// memory, whose name reflects the shared copy's name and sharedAt the share time.
System.out.println("=== Memory Sharing ===");
try {
// Share with the whole account/team (the optional argument names the shared copy)
Memory teamShare = lara.memories.addAccountShare(memoryId, "Shared with the team");
System.out.println("🤝 Shared with the account as: '" + teamShare.getName() + "' (shared at " + teamShare.getSharedAt() + ")");
// Rename the account/team share
Memory renamedTeamShare = lara.memories.renameAccountShare(memoryId, "Team memory");
System.out.println("📝 Renamed account share to: '" + renamedTeamShare.getName() + "'");
// List every share visible to the caller: the account share, group shares and user shares
MemoryShares shares = lara.memories.getShares(memoryId);
if (shares.getAccount() != null) {
System.out.println("👥 Account share '" + shares.getAccount().getShareName() + "' (" + shares.getAccount().getPermissions() + ")");
}
for (ResourceShareEntry group : shares.getGroups()) {
System.out.println("👥 Group " + group.getName() + ": '" + group.getShareName() + "' (" + group.getPermissions() + ")");
}
for (ResourceShareEntry user : shares.getUsers()) {
System.out.println("👤 User " + user.getName() + ": '" + user.getShareName() + "' (" + user.getPermissions() + ")");
}
// Revoke the account/team share
lara.memories.revokeAccountShare(memoryId);
System.out.println("🚫 Revoked the account share");
// Group shares work the same way, addressed by a group ID (grp_...)
String groupId = System.getenv("LARA_GROUP_ID"); // Replace with an actual group ID
if (groupId != null) {
Memory groupShare = lara.memories.addGroupShare(memoryId, groupId, "Shared with the group");
System.out.println("🤝 Shared with group " + groupId + " as: '" + groupShare.getName() + "'");
lara.memories.renameGroupShare(memoryId, groupId, "Marketing group");
System.out.println("📝 Renamed the group share");
lara.memories.revokeGroupShare(memoryId, groupId);
System.out.println("🚫 Revoked the group share");
} else {
System.out.println("Set LARA_GROUP_ID to try the group sharing methods.");
}
System.out.println();
} catch (LaraException e) {
System.out.println("Error sharing memory: " + e.getMessage() + "\n");
}
} catch (LaraException e) {
System.out.println("Error creating memory: " + e.getMessage() + "\n");
return;
} finally {
// Cleanup
System.out.println("=== Cleanup ===");
if (memoryId != null) {
try {
Memory deletedMemory = lara.memories.delete(memoryId);
System.out.println("🗑️ Deleted memory: " + deletedMemory.getName());
} catch (LaraException e) {
System.out.println("Error deleting memory: " + e.getMessage());
}
}
if (externalMemoryId != null) {
try {
Memory deletedExternalMemory = lara.memories.delete(externalMemoryId);
System.out.println("🗑️ Deleted external memory: " + deletedExternalMemory.getName());
} catch (LaraException e) {
System.out.println("Error deleting external memory: " + e.getMessage());
}
}
if (memory2ToDelete != null) {
try {
Memory deletedMemory2 = lara.memories.delete(memory2ToDelete);
System.out.println("🗑️ Deleted second memory: " + deletedMemory2.getName());
} catch (LaraException e) {
System.out.println("Error deleting second memory: " + e.getMessage());
}
}
}
System.out.println("\n🎉 Memory management examples completed!");
}
}