-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndEdit.java
More file actions
68 lines (51 loc) · 1.93 KB
/
Copy pathEndEdit.java
File metadata and controls
68 lines (51 loc) · 1.93 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
package turing;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import turing.Message.OPERATION;
import turing.Message.RESULT;
public class EndEdit implements Runnable {
private SocketChannel client;
private Message received;
public EndEdit(SocketChannel client, Message received){
this.client = client;
this.received = received;
}
public void run() {
String clientUsername = received.getUsername();
String documentName = received.getDocumentName();
int numeroSezione = received.getNumOfSection();
Section editing = null;
Document document = null;
Message.RESULT result;
try {
if ( (result = Turing.databaseUsers.haveDocument(clientUsername,documentName)) == RESULT.OP_OK)
{
document = Turing.databaseDocuments.getDocument(documentName);
editing = document.getSection(numeroSezione);
if (editing != null && editing.isEditing(clientUsername))
{
FileChannel fileChannel = FileChannel.open(Paths.get(Turing.DATA_DIR,editing.getFileName()),StandardOpenOption.CREATE,StandardOpenOption.WRITE,StandardOpenOption.TRUNCATE_EXISTING);
ByteBuffer read = Message.read(client); //leggo il contenuto
//la Message.read fa la flip!
if (Message.writeFileChannel(read, fileChannel)){
result = editing.endEdit(clientUsername);
fileChannel.close();
}
read.clear();
}
}
Message toSend = new Message();
toSend.setMessageResult(OPERATION.EDIT_DOC,result);
Message.sendMessage(client, toSend);
Turing.databaseDocuments.getDocument(document.getDocName()).getNumberOfEditing().decrementAndGet();
System.out.println("Server-EndEdit ["+ received.getUsername()+", "+received.getDocumentName() +"]: " +result);
client.close();
} catch (IOException e){
e.printStackTrace();
}
}
}