-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSection.java
More file actions
72 lines (56 loc) · 1.55 KB
/
Copy pathSection.java
File metadata and controls
72 lines (56 loc) · 1.55 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
package turing;
import java.io.File;
import java.io.IOException;
import turing.Message.RESULT;
public class Section implements Comparable<Section> {
private String fileName; //filename
private String activeUser;
private int numSec;
public Section(String doc, int index){
this.fileName = new String(doc+"_"+index);
this.activeUser = null;
this.numSec = index;
}
public synchronized Message.RESULT startEdit(String user){
try {
if (this.activeUser != null) return RESULT.SEC_EDITING;
this.activeUser = user;
String directorypath = Turing.DATA_DIR;
File dir = new File(directorypath);
if (!dir.exists()) dir.mkdirs();
String filepath = directorypath+ this.fileName;
File file = new File(filepath);
if (!file.exists()) file.createNewFile();
return RESULT.OP_OK;
} catch (IOException e){
e.printStackTrace();
return null;
}
}
public synchronized Message.RESULT endEdit(String user){
if (this.activeUser == null) return RESULT.OP_ERR;
if (! (this.activeUser.equals(user)) )
return RESULT.NOT_AUTORIZED;
else
this.activeUser = null;
return RESULT.OP_OK;
}
public synchronized boolean isEditing(String user){
if (this.activeUser == null || ! (this.activeUser.equals(user)) || user == null )
return false;
return true;
}
public String getFileName(){
return this.fileName;
}
public String getActiveUser(){
return this.activeUser;
}
public int getNumSec() {
return numSec;
}
@Override
public int compareTo(Section o) {
return this.fileName.compareTo(o.fileName);
}
}