-------------------------------------------------------------------------------- -- Title: LUPNote.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies require( "LUObject" ) require( "LUDate" ) require( "LFSFile" ) require( "MIME" ) require( "uuid" ) require( "md5" ) -- extend the super class local thisClass, superClass = LUObject.extend( LUObject ) -- factory class method, this is where all the instance methods are implemented as closures thisClass.new = function( aUser, aDate ) -- extend the super class instance local this = superClass.extend( superClass.new() ) -- instance variables local ivars = { user = aUser, date = aDate } -- instance method this.content = function() if ( ivars.content == nil ) then this.headers() end if ( ( ivars.content == nil ) or ( string.len( ivars.content ) == 0 ) ) then ivars.content = "empty note" end return ivars.content end -- instance method this.setContent = function( aValue ) if ( this.content() ~= aValue ) then ivars.content = aValue end return this end -- instance method this.contentType = function() local aValue = this.headers().get( "content-type" ) if ( ( aValue == nil ) or ( string.len( aValue) == 0 ) ) then aValue = "text/x-markdown; charset=utf-8" end return aValue end -- instance method this.changeDate = function() local aFile = this.file() local someAttributes = aFile.attributes() if ( someAttributes ~= nil ) then local aTime = someAttributes.get( "modification" ) if ( aTime ~= nil ) then return LUDate.new( aTime ) end end return this.creationDate() end -- instance method this.creationDate = function() return LUDate.new( uuid.time( this.uuid() ) ) end -- instance method this.date = function() if ( ivars.date == nil ) then ivars.date = LUDate.new() end return ivars.date end -- instance method this.setDate = function( aValue ) if ( this.date().equals( aValue ) == false ) then local aFile = this.file() local anID = this.id() local anotherNote = thisClass.new( this.user(), aValue ) local anotherFile = anotherNote.file() local anotherID = anotherNote.id() local aDirectory = LFSFile.new( anotherFile.directory() ) aDirectory.mkdirs() LULog.debug( aFile ) LULog.debug( anotherFile ) if ( aFile.move( anotherFile ) == true ) then aDirectory = LFSFile.new( aFile.directory() ) while ( true ) do if ( aDirectory.delete() == true ) then if ( aDirectory.parentDirectory() ~= nil ) then aDirectory = LFSFile.new( aDirectory.parentDirectory() ) else break end else break end end this.user().noteDidChange( anotherNote, anID ) return anotherNote else LULog.error( "failed to move '" .. aFile.toString() .. "' to '" .. anotherFile.toString() .. "'" ) end end return this end -- instance method to compare the given object with this object for equality this.equals = function( anObject ) if ( this == anObject ) then return true end if ( this.isKindOf( anObject, this ) == true ) then if ( this.date().equals( anObject.date() ) == true ) then return true end end return false end -- instance method this.id = function() return string.format( "%x", this.date().time() ) end -- instance method this.headers = function() if ( ivars.headers == nil ) then local aContent = this.file().content() ivars.headers, ivars.content = MIME.toMIME( aContent ) end return ivars.headers end -- instance method this.messageID = function() local aValue = this.headers().get( "message-id" ) if ( ( aValue == nil ) or ( string.len( aValue) == 0 ) ) then aValue = "<" .. this.uid() .. "@" .. this.user().host() .. ">" end return aValue end -- private instance method local name = function() return this.date().toString( "%H-%M-%S" ) end -- private instance method local path = function() local aSeparator = LFSFile.separator() local aFormat = "%Y" .. aSeparator .. "%m" .. aSeparator .. "%d" local aPath = this.date().toString( aFormat ) return aPath end -- instance method this.file = function() local aSeparator = LFSFile.separator() local aPath = this.user().directory().path() .. aSeparator .. path() local aName = name() .. ".txt" local aFile = LFSFile.new( aPath, aName ) return aFile end -- instance method this.store = function() local aFile = this.file() if ( aFile.exists() == false ) then LFSFile.new( aFile.directory() ).mkdirs() end aFile.setContent( MIME.toString( this.headers(), this.content() ) ) this.user().noteDidChange( this ) return this end -- instance method this.subject = function() local aValue = this.headers().get( "subject" ) if ( ( aValue == nil ) or ( string.len( aValue) == 0 ) ) then aValue = this.date().toString( "%A, %B %d, %Y" ) aValue = string.lower( aValue ) this.headers().put( "subject", aValue ) end return aValue end -- instance method this.setSubject = function( aValue ) if ( this.subject() ~= aValue ) then this.headers().put( "subject", aValue ) end return this end -- instance method this.toString = function() return this.date().toString() end -- instance method this.user = function() return ivars.user end -- instance method this.uid = function() local aValue = this.headers().get( "x-uid" ) if ( ( aValue == nil ) or ( string.len( aValue) == 0 ) ) then aValue = md5.digest( this.uuid() ) end return aValue end -- instance method this.uuid = function() local aValue = this.headers().get( "x-uuid" ) if ( ( aValue == nil ) or ( string.len( aValue) == 0 ) ) then aValue = tostring( uuid.new( "time" ) ) this.headers().put( "x-uuid", aValue ) end return aValue end -- instance method this.majorVersion = function() local anEpoch = os.time( { year = 2000, month = 1, day = 1, hour = 0, min = 0, sec = 0 } ) local aDayInterval = 60 * 60 * 24 local aDayDate = this.date().dayDate() local anInterval = math.floor( ( aDayDate.time() - anEpoch ) / aDayInterval ) return anInterval end -- instance method this.minorVersion = function() local aDate = this.date() local anInterval = ( aDate.hour() * 60 ) + aDate.minute() return anInterval end -- instance method this.microVersion = function() return this.date().second() end -- instance method this.version = function() return this.majorVersion() .. "." .. this.minorVersion() .. "." .. this.microVersion() end -- return the public instance methods return superClass.inherit( this ) end -- class method thisClass.noteWithDate = function( aUser, aDate ) local aNote = thisClass.new( aUser, aDate ) local aFile = aNote.file() local someAttributes = aFile.attributes() if ( ( someAttributes ~= nil ) and ( someAttributes.get( "mode" ) == "file" ) ) then return aNote end return nil end -- class method thisClass.noteWithID = function( aUser, anID ) local aTime = tonumber( anID, 16 ) local aDate = LUDate.new( aTime ) LULog.debug( aUser ) LULog.debug( anID ) LULog.debug( aTime ) LULog.debug( aDate ) return thisClass.noteWithDate( aUser, aDate ) end -- define the class name LUPNote = thisClass return LUPNote