module File;

export File;

from LectureDB import UserWorkSpace, Source, PreviousState;
from View impost Layer;
from Tools import PostItLog;
from Roster import RosterDB;
from QandA import QuestionLogDB;

object FileSpace isFile*;
description: (*A FileSpace is an abstract model of a file space in the operating environment in which the Eclass Tool is run. The 		   
*FileSpace is simplya collection of zero or more Files, with no other properties modeledhere.*);
*end;

object File is
components: name:FileName and permissions:FilePermissions and
file_type:FileType and data:FileData;
description: (*
A File is an abstraction of a file stored in the file space. It has a
name, permissions, type, and data. These are the components sufficient
to specify the behavior of Eclass Tool file operations.
*);
end File;

object FileType is lect_type:LectureType or layer_type:LayerType or postIts_type:PostItsType
  or access_type:AccessType or questLog_type:QuestLogType or notepad_type:NotepadType or 
  other_type:OtherType
   description: (*
	The type of file data Eclass users are interested in is LectureType, LayerType, 
	PostItsType, QuestLogType or NotepadType. Files that contain any other type of data
	are considered OtherType which we don't care about.
*);
end FileType;

object FileName is string
description: (*The name of a file. The string representation here is an abstraction
of file names used in specific operating environments. Implementations
may obey any syntactic or semantic constraints imposed by a particular
environment.
*);
end;

object FileData is lect:Source or layer:Layer or postIts:PostItLog or access:RosterDB 
  or questLog:QuestionLogDB or notepad:Notepad
   description: (*
	The abstract representation of Eclass FileData can be a Source, PrivateLayer, 
	PublicLayer, PostItLog, RosterDB, QuestionLogDB or Notepad object. Multiple 
	objects are required because each of these items is saved as a separate HTML or 
	PDF file. Source is all the HTML for a Lecture. Layer is the public layer. 
	RosterDB contains access rights. 
*);
end FileData;

object LectureType
  description: (*
        File data typing tag indicating that a file contains lecture data created by the 
        Eclass tool.
*);
end LectureType;

object LayerType
 description:(* File data indicating a layer was created by the Eclass Tool*);
end LayerType;

object PostItsType
	description: (* File data indicating a post it was created by the Eclass tool*);
end PostItsType;

object AccessType
description: (*file data indicating a user access by the Eclass tool*);
end AccessType;

object QuestLogType
description: (*file data indicating the question log was created by the Eclass tool*);
end QuestLogType;

object NotepadType
description: (* file data indicating the notepad was created by the Eclass tool*);
end NotepadType:

object OtherType
description: (*file data indicating what type or if the file was created by the Eclass tool*);
end OtherType;

operation FileOpen is
inputs: fs:FileSpace, fn:FileName, uws:UserWorkSpace;
outputs: fs’:FileSpace, uws’:UserWorkSpace;
description: (*
Open an existing lecture file of the given name and put the data from
that file in the workspace.
*);
precondition:
(*
*A file of the given name exists in the given file space, the file
*is readable, and the file’s data are of type Eclass.
*)
exists (file in fs)
(file.name = fn) and
file.permissions.is_readable and
file.file_type?lecture_type;
postcondition:
(*
*The output workspace has a new lecture containing the file data of
*the input file, and that lecture is current and the previous lecture is closed leaving only one open lecture at a time.
*The user id of the new lecture is that of the workspace, and the lecture does not require saving.
*)
(exists (sf: SavedFiles)
(sf.source = uws’.lect.source) and
(exists (file in fs)
(file.name = fn) and
(sf.file = file) and
(sf = file.data)
)and
(not sf.requires_saving) and
)
);
end FileOpen;

operation FileClose is
inputs: fs:FileSpace, uws:UserWorkSpace;
outputs: fs’:FileSpace, uws’:UserWorkSpace;
description: (*
Close the current lecture if it does not require saving.
*);
precondition
(*
*The lecture does not require saving.
*)
not (uws.lect.source.requires_saving);
postcondition:
(*
*The current lecture is deleted from the workspace. 
*)
uws’.lect = nil;
);
end FileClose;

operation FileSave is
inputs: fs:FileSpace, uws:UserWorkSpace;
outputs: fs’:FileSpace, uws’:UserWorkSpace;
description: (*
If the lectures in the given workspace requires saving, save it in the
given file space.
*);
precondition:
(**The given workspace requires saving. Also, there is a writable
*file of the current workspace filename in the given FileSpace. Note
*that the only way the current file could be unwritable is through an
*external change to the file space since the file was opened by the
*Eclass Tool. Note further that this precondition disallows the
*case where the current lecture file has been externally deleted
*since it was opened by the Eclass tool. That is, the file must
*both exist and be writable at the time the save is attempted.  
*The same must be done for all other files to be saved (in SavedFiles).
*)
(uws.lect.source.requires_saving;
and
(exists (file in fs)
(file.name = uws.lect.source.file.name) and
(file.permissions.is_writable));
postcondition:
(*
*There is a lecture-type file in the resulting FileSpace containing
*the current workspace lecture as its file data. In the resulting
*workspace, the requires saving indicator and is false.
*)
(exists (file in fs’)
(file.name = uws’.lect.source.file.name) and
(file.permissions.is_writable) and
(file.file_type?lecture_type) and
(not uws’.lect.source.requires_saving)
);
end FileSave;

object SavedFiles is 
components: PrivateNotes and PostItLog and PublicLayer or QandALog or Notepad or source:Source;
description: (*Holds all files that are needed to be saved including Post-it log, Notepad, Q&A which 
*will be in HTML; Private Notes and Public Layer which will be in PDF*)
end SavedFiles;

end File;