File:  [Repository] / cdli / cdli_files.py
Revision 1.84: download - view: text, annotated - select for diffs - revision graph
Thu Sep 25 13:41:58 2008 UTC (15 years, 9 months ago) by dwinter
Branches: MAIN
CVS tags: HEAD
correction in managing the store

    1: """CDLI extensions of the filearchive"""    
    2: from Products.versionedFile.extVersionedFile import *
    3: from Products.ZCatalog.CatalogPathAwareness import CatalogAware
    4: from tempfile import mkstemp,mkdtemp    
    5: import os.path
    6: import os
    7: from types import *
    8: import urlparse
    9: import urllib
   10: import cgi
   11: from OFS.OrderedFolder import OrderedFolder
   12: from OFS.SimpleItem import SimpleItem
   13: import time
   14: from OFS.Folder import manage_addFolder
   15: import re
   16: from AccessControl import ClassSecurityInfo
   17: from Acquisition import Implicit
   18: from Globals import Persistent
   19: from threading import Thread
   20: from ZPublisher.HTTPRequest import HTTPRequest
   21: from ZPublisher.HTTPResponse import HTTPResponse
   22: from ZPublisher.BaseRequest import RequestContainer
   23: import threading
   24: from BTrees.OOBTree import OOBTree, OOTreeSet
   25: import logging
   26: import transaction
   27: import copy
   28: import codecs
   29: import sys
   30: from BTrees.IOBTree import IOBTree 
   31: import cdliSplitter
   32: from sets import Set
   33: import md5
   34: 
   35: def unicodify(s):
   36:     """decode str (utf-8 or latin-1 representation) into unicode object"""
   37:     if not s:
   38:         return u""
   39:     if isinstance(s, str):
   40:         try:
   41:             return s.decode('utf-8')
   42:         except:
   43:             return s.decode('latin-1')
   44:     else:
   45:         return s
   46: 
   47: def utf8ify(s):
   48:     """encode unicode object or string into byte string in utf-8 representation.
   49:        assumes string objects to be utf-8"""
   50:     if not s:
   51:         return ""
   52:     if isinstance(s, str):
   53:         return s
   54:     else:
   55:         return s.encode('utf-8')
   56: 
   57: def formatAtfHtml(l):
   58:     """escape special ATF characters for HTML"""
   59:     if not l:
   60:         return ""
   61: 
   62:     # replace &
   63:     l = l.replace('&','&')
   64:     # replace angular brackets
   65:     l = l.replace('<','&lt;')
   66:     l = l.replace('>','&gt;')
   67:     return l
   68: 
   69: def formatAtfLineHtml(l, nolemma=True):
   70:     """format ATF line for HTML"""
   71:     if not l:
   72:         return ""
   73: 
   74:     if nolemma:
   75:         # ignore lemma lines
   76:         if l.lstrip().startswith('#lem:'):
   77:             return ""
   78:     
   79:     return formatAtfHtml(l)
   80: 
   81: 
   82: 
   83: def formatAtfFullLineNum(txt, nolemma=True):
   84:     """format full line numbers in ATF text"""
   85:     # surface codes
   86:     surfaces = {'@obverse':'obv',
   87:                 '@reverse':'rev',
   88:                 '@surface':'surface',
   89:                 '@edge':'edge',
   90:                 '@left':'left',
   91:                 '@right':'right',
   92:                 '@top':'top',
   93:                 '@bottom':'bottom',
   94:                 '@face':'face',
   95:                 '@seal':'seal'}
   96: 
   97:     if not txt:
   98:         return ""
   99:     
  100:     ret = []
  101:     surf = ""
  102:     col = ""
  103:     for line in txt.splitlines():
  104:         line = unicodify(line)
  105:         if line and line[0] == '@':
  106:             # surface or column
  107:             words = line.split(' ')
  108:             if words[0] in surfaces:
  109:                 surf = line.replace(words[0],surfaces[words[0]]).strip()
  110:             
  111:             elif words[0] == '@column':
  112:                 col = ' '.join(words[1:])
  113:             
  114:         elif line and line[0] in '123456789':
  115:             # ordinary line -> add line number
  116:             line = "%s:%s:%s"%(surf,col,line)
  117:             
  118:         ret.append(line)
  119:     
  120:     return '\n'.join(ret)
  121:             
  122:             
  123: def generateXMLReturn(hash):
  124:     """erzeugt das xml file als returnwert fuer uploadATFRPC"""
  125: 
  126:     ret="<return>"
  127:     
  128:     ret+="<errors>"
  129:     for error in hash['errors']:
  130:         ret+="""<error atf="%s">%s</error>"""%error
  131:     
  132:     ret+="</errors>"
  133:     
  134:     ret+="<changes>"
  135:     for changed in hash['changed']:
  136:         ret+="""<change atf="%s">%s</change>"""%changed
  137:     ret+="</changes>"
  138:     
  139:     ret+="<newPs>"
  140:     for new in hash['newPs']:
  141:         ret+="""<new atf="%s"/>"""%new
  142:     ret+="</newPs>"
  143:     
  144:     ret+="</return>"
  145:     return ret
  146:     
  147:     
  148: def unique(s):
  149:     """Return a list of the elements in s, but without duplicates.
  150: 
  151:     For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3],
  152:     unique("abcabc") some permutation of ["a", "b", "c"], and
  153:     unique(([1, 2], [2, 3], [1, 2])) some permutation of
  154:     [[2, 3], [1, 2]].
  155: 
  156:     For best speed, all sequence elements should be hashable.  Then
  157:     unique() will usually work in linear time.
  158: 
  159:     If not possible, the sequence elements should enjoy a total
  160:     ordering, and if list(s).sort() doesn't raise TypeError it's
  161:     assumed that they do enjoy a total ordering.  Then unique() will
  162:     usually work in O(N*log2(N)) time.
  163: 
  164:     If that's not possible either, the sequence elements must support
  165:     equality-testing.  Then unique() will usually work in quadratic
  166:     time.
  167:     (from the python cookbook)
  168:     """
  169: 
  170:     n = len(s)
  171:     if n == 0:
  172:         return []
  173: 
  174:     # Try using a dict first, as that's the fastest and will usually
  175:     # work.  If it doesn't work, it will usually fail quickly, so it
  176:     # usually doesn't cost much to *try* it.  It requires that all the
  177:     # sequence elements be hashable, and support equality comparison.
  178:     u = {}
  179:     try:
  180:         for x in s:
  181:             u[x] = 1
  182:     except TypeError:
  183:         del u  # move on to the next method
  184:     else:
  185:         return u.keys()
  186: 
  187:     # We can't hash all the elements.  Second fastest is to sort,
  188:     # which brings the equal elements together; then duplicates are
  189:     # easy to weed out in a single pass.
  190:     # NOTE:  Python's list.sort() was designed to be efficient in the
  191:     # presence of many duplicate elements.  This isn't true of all
  192:     # sort functions in all languages or libraries, so this approach
  193:     # is more effective in Python than it may be elsewhere.
  194:     try:
  195:         t = list(s)
  196:         t.sort()
  197:     except TypeError:
  198:         del t  # move on to the next method
  199:     else:
  200:         assert n > 0
  201:         last = t[0]
  202:         lasti = i = 1
  203:         while i < n:
  204:             if t[i] != last:
  205:                 t[lasti] = last = t[i]
  206:                 lasti += 1
  207:             i += 1
  208:         return t[:lasti]
  209: 
  210:     # Brute force is all that's left.
  211:     u = []
  212:     for x in s:
  213:         if x not in u:
  214:             u.append(x)
  215:     return u
  216: 
  217: 
  218: class BasketContent(SimpleItem):
  219:     """classe fuer den Inhalt eines Baskets"""
  220:    
  221:     def __init__(self,content=[]):
  222:         """content"""
  223:         self.contentList=content[0:]
  224:     
  225:     def getContent(self):
  226:         """get content"""
  227: 
  228:         return self.contentList
  229:     
  230:     def setContent(self,content):
  231:         self.contentList=content[0:]
  232:     
  233:     def numberOfItems(self):
  234:         """number"""
  235:         
  236:         return len(self.getContent())
  237:         
  238:     
  239: class uploadATFfinallyThread(Thread):
  240:     """class for adding uploaded filed (temporarily stored in the staging area at /tmp"""
  241:     
  242:     def __init__(self):
  243:         """init for uploadATFfinallyThread"""
  244:         self.continueVar=True
  245:         self.returnValue=None
  246:         self.end=False
  247:         Thread.__init__(self)
  248:            
  249:     def set(self,procedure,comment="",basketname='',unlock=None,SESSION=None,username=None,serverport="8080"):
  250:         """set start values for the thread"""
  251:         self.procedure=procedure
  252:         self.comment=comment
  253:         self.basketname=basketname
  254:         self.unlock=unlock
  255:         self.SESSION=SESSION
  256:         self.username=username
  257:         self.serverport=serverport
  258:        
  259:         
  260:     def __call__(self):
  261:         """call of the thread (equals run)"""
  262:         self.run()
  263:         return True
  264:     
  265:     def getContext(self, app,serverport="8080"):
  266:         """get the context within the ZODB"""
  267:         
  268:         resp = HTTPResponse(stdout=None)
  269:         env = {
  270:             'SERVER_NAME':'localhost',
  271:             'SERVER_PORT':serverport,
  272:             'REQUEST_METHOD':'GET'
  273:             }
  274:         req = HTTPRequest(None, env, resp)
  275:         return app.__of__(RequestContainer(REQUEST = req))
  276:           
  277:         
  278:     def run(self):
  279:         """run"""
  280:         
  281:         self.result=""
  282:         #find context within ZODB
  283:         from Zope import DB
  284:         conn = DB.open()
  285:         root = conn.root()
  286:         app  = root['Application']
  287:         ctx = self.getContext(app,serverport=self.serverport)
  288: 
  289:         #add the files
  290:         self.uploadATFfinallyThread(ctx,self.procedure,comment=self.comment,basketname=self.basketname,unlock=self.unlock,SESSION=self.SESSION,username=self.username)
  291:         #commit the transactions
  292:         transaction.get().commit()
  293:         conn.close()
  294:         #set flag for end of this method
  295:         self.end=True
  296:         logging.info("ended")
  297:         return True
  298:     
  299:     def __del__(self):
  300:         """delete"""
  301:         
  302:         
  303:     
  304:     def getResult(self):
  305:         """method for accessing result"""
  306:         
  307:         return self.result
  308:      
  309:     def uploadATFfinallyThread(self,ctx,procedure,comment="",basketname='',unlock=None,RESPONSE=None,SESSION=None,username=None):
  310:         """upload the files"""
  311:         #TODO: make this configurable, at the moment, rootFolder for cdli has to be cdliRoot
  312:         ctx2=ctx.cdliRoot
  313:    
  314:         self.result+="<h2>Start processing</h2>"
  315:         
  316:         #shall I only upload the changed files?
  317:         logging.debug("uploadATFfinally procedure: %s"%procedure)
  318:         if procedure=="uploadchanged":
  319:             changed=[x[0] for x in SESSION.get('changed',[])]
  320:             uploadFns=changed+SESSION.get('newPs',[])
  321:         
  322:         #or all
  323:         elif procedure=="uploadAll":
  324:             uploadFns=[]
  325:             for x in os.listdir(SESSION['tmpdir']):
  326:                 if not x in SESSION['lockerrors']:
  327:                     uploadFns.append(x)
  328:                     
  329:         #or maybe nothing
  330:         elif procedure=="noupload":
  331:             return True
  332:         else:
  333:             uploadFns=[]
  334:             
  335:         #do first the changed files    
  336:         i=0
  337:         for fn in uploadFns:
  338:             logging.debug("uploadATFfinally uploadFn=%s"%fn)
  339:             i+=1
  340:             founds=ctx2.CDLICatalog.search({'title':fn})
  341:             if len(founds)>0:
  342:                 SESSION['author']=str(username)
  343:                 self.result="<p>Changing : %s"%fn+self.result
  344:                 logging.debug("uploadatffinallythread changing:%s"%fn+self.result)
  345:                 founds[0].getObject().manage_addCDLIFileObject('',comment,SESSION['author'],file=os.path.join(SESSION['tmpdir'],fn),from_tmp=True)
  346:             if i%200==0:
  347:                 transaction.get().commit()
  348:                 logging.debug("uploadatffinallythread changing: do commit")
  349:         
  350:         transaction.get().commit()
  351:         logging.debug("uploadatffinallythread changing: last commit")
  352: 
  353:         #now add the new files        
  354:         newPs=SESSION['newPs']
  355:         if len(newPs)>0:
  356:             tmpDir=SESSION['tmpdir']
  357:             logging.debug("uploadatffinallythread adding start")
  358:             self.result="<p>Adding files</p>"+self.result
  359:             #TODO: make this configurable, at the moment base folder for the files has to be cdli_main
  360:             ctx2.importFiles(comment=comment,author=str(username) ,folderName=tmpDir, files=newPs,ext=self)
  361:             logging.debug("uploadatffinallythread adding finished")
  362:         
  363:         #unlock locked files?
  364:         if unlock:
  365:             logging.debug("uploadatffinallythread unlocking start")
  366:             self.result="<p>Unlock files</p>"+self.result
  367:             unlockFns=[]
  368:             for x in os.listdir(SESSION['tmpdir']):
  369:                     if not x in SESSION['errors']:
  370:                         unlockFns.append(x)
  371:                         
  372:             logging.debug("unlocking have now what to unlock")
  373:                         
  374:             for fn in unlockFns:
  375:                 #logging.info("will unlock: %s"%fn)
  376:                 founds=ctx2.CDLICatalog.search({'title':fn})
  377:                 #logging.info("found it: %s"%repr(founds))
  378:                 if len(founds)>0:
  379:                     #logging.info("unlock: %s"%founds[0].getObject().getId())
  380:                     SESSION['author']=str(username)
  381:                     founds[0].getObject().lockedBy=""
  382: 
  383:             logging.debug("uploadatffinallythread unlocking done")
  384:                     
  385:         #if a basketname is given, add files to the basket
  386:         if not (basketname ==''):
  387:             logging.debug("uploadatffinallythread add to basket %s"%basketname)
  388:             self.result="<p>Add to basket</p>"+self.result
  389:             basketId=ctx2.basketContainer.getBasketIdfromName(basketname)
  390:             
  391:             if not basketId: # create new basket
  392:                 logging.debug("uploadatffinallythread create basket %s"%basketname)
  393:                 self.result="<p>Create a new basket</p>"+self.result
  394:                 ob=ctx2.basketContainer.addBasket(basketname)
  395:                 basketId=ob.getId()
  396:             basket=getattr(ctx2.basketContainer,str(basketId))
  397:             ids=os.listdir(SESSION['tmpdir'])
  398:             basket.addObjects(ids,deleteOld=True,username=str(username))    
  399:                
  400:         logging.debug("uploadatffinallythread uploadfinally done")
  401: 
  402:         if RESPONSE is not None:
  403:             RESPONSE.redirect(self.aq_parent.absolute_url())
  404:         
  405:         return True
  406: 
  407: class tmpStore(SimpleItem):
  408:     """simple item"""
  409:     meta_type="cdli_upload"
  410:     
  411:     def __init__(self,id):
  412:         """init tmp"""
  413:         self.id=id
  414:         
  415: class uploadATFThread(Thread):
  416:     """class for checking the files befor uploading"""
  417:     
  418:     def __init__(self):
  419:         """initialise"""
  420:         
  421:         self.continueVar=True
  422:         self.returnValue=None
  423:         
  424:         Thread.__init__(self)
  425:         
  426:         
  427:     def set(self,upload,basketId,username,idTmp,serverport="8080"):
  428:         """set start values for the thread"""
  429:         self.result=""
  430:         self.upload=upload
  431:         self.basketId=basketId
  432:         self.username=username
  433:         self.serverport=serverport
  434:         self.idTmp=idTmp
  435:         
  436:     def __call__(self):
  437:         """call method """
  438:         self.run()
  439:         return True
  440:     
  441:     def getContext(self, app,serverport="8080"):
  442:         """get the context within the ZODB"""
  443:         resp = HTTPResponse(stdout=None)
  444:         env = {
  445:             'SERVER_NAME':'localhost',
  446:             'SERVER_PORT':serverport,
  447:             'REQUEST_METHOD':'GET'
  448:             }
  449:         req = HTTPRequest(None, env, resp)
  450:         return app.__of__(RequestContainer(REQUEST = req))
  451:         
  452:     def run(self):
  453:         idTmp=self.idTmp
  454:         self.result=""
  455:         #find context within ZODB
  456:         from Zope import DB
  457:         conn = DB.open()
  458:         root = conn.root()
  459:         app  = root['Application']
  460:         ctx = self.getContext(app,serverport=self.serverport)
  461:         logging.info("run intern")
  462:         try:
  463:             logging.info("created: %s"%idTmp)
  464:             ctx.temp_folder._setObject(idTmp,tmpStore(idTmp))
  465:         except:
  466:             logging.error("thread upload: %s %s"%sys.exc_info()[0:2])
  467:             
  468:         logging.info("call thread intern")
  469:         self.uploadATFThread(ctx,self.upload,idTmp,self.basketId)
  470:      
  471:         #ctx.cdliRoot.cdli_main.tmpStore2[self.getName()[0:]]=self.returnValue
  472:         
  473:         
  474:         transaction.get().commit()
  475:        
  476:         conn.close()
  477:         
  478:         return getattr(ctx.temp_folder,idTmp)
  479:         
  480:     def getResult(self):
  481:         """method for accessing result"""
  482:         return self.result
  483:     
  484:     def uploadATFThread(self,ctx,upload,idTmp,basketId=0):
  485:         """upload an atf file"""
  486:         #TODO: add comments
  487:         #TODO: finish uploadATF
  488:         
  489:         stObj=getattr(ctx.temp_folder,idTmp)
  490:         logging.info("start, upload thread")
  491:         self.result="<html><body><h2>I got your file, start now to split it into single atf-files!</h2><p>"
  492:     
  493:         #make sure that id is a string and not an integer
  494:         basketId=str(basketId)
  495:         
  496:         #TODO: make this configurable, at the moment, rootFolder for cdli has to be cdliRoot
  497:         ctx2=ctx.cdliRoot
  498:         
  499:         #get temporary file for staging the downloaded and splitted files
  500:         dir=mkdtemp()
  501:         
  502:         
  503:         changed=[] # changed files
  504:         errors=[]  # files with errors
  505:         lockerrors=[]  # files with errors
  506: 
  507:         newPs=[]   # new p filed
  508:         psNotInCatalog=[] # files not in the catalog
  509:         
  510:         #split the uploadedd atf file
  511:         basketNameFromFile, numberOfFiles=splitatf(upload,dir,ext=self)
  512:         
  513:         #find basketId if not set
  514:         
  515:         #get active abaket
  516:         if basketId == '0':
  517:             basketObj=ctx2.basketContainer.getActiveBasket()
  518:             if basketObj:
  519:                 basketId=basketObj.getId()
  520:                 
  521:         #if there is no active basket and no basketid given, id is empty, else get besketname and length
  522:         if basketId == '0':
  523:             basketNameFromId=""
  524:             basketLen=0
  525:         else:
  526:             basketNameFromId=getattr(ctx2.basketContainer,basketId).title
  527:             basketLen=getattr(ctx2.basketContainer,basketId).getLastVersion().numberOfItems()
  528:             
  529:         logging.info("got the file, upload thread")
  530:         self.result+="""<html><body><h2>I got the files</h2><
  531:                         p>I am computing the differences to the exisiting files</p>"""
  532:                                    
  533:         #start to check the files
  534:         for fn in os.listdir(dir):
  535:             
  536:             self.result="<p>process:%s</p>"%fn+self.result
  537:             
  538:             # check if file is in the catalog
  539:             #TODO: checkCatalog is not implemented yet
  540:             if ctx2.cdli_main.checkCatalog(fn):
  541:                 psNotInCatalog.append(fn)
  542:                 
  543:             #check if p-file already at the server  
  544:             founds=ctx2.CDLICatalog.search({'title':fn})    
  545:       
  546:             #if not than add filename to the list of newfiles
  547:             
  548:             data=file(os.path.join(dir,fn)).read()
  549:             status,msg=checkFile(fn,data,dir)
  550:             #status=True
  551:             
  552:             
  553:             if not status: # error
  554:                 errors.append((fn,msg))
  555:             
  556:             else:
  557:                 if len(founds)==0:
  558:                     newPs.append(fn)
  559: 
  560:                 #if p file alread at the server    
  561:                 for found in founds:
  562:                     #analyse the differences to the actual file
  563:                     obj=found.getObject()
  564: 
  565:                     if (not (str(obj.lockedBy))=='') and (not (str(obj.lockedBy)==str(self.username))):
  566:                                 lockerrors.append((fn,str(obj.lockedBy)))
  567:                     else:
  568:                 
  569:                         diffs=obj.diff(data)
  570:                         if diffs[0]>0:
  571:                             changed.append((obj,diffs)) #hochladen
  572: 
  573:         #ready, set the returnValues
  574:         self.result+="<h3>Done</h3></body></html>"
  575:         
  576:         stObj.returnValue={}
  577:         
  578:         stObj.returnValue['errors']=errors
  579:         
  580:         stObj.returnValue['newPs']=newPs
  581:         stObj.returnValue['tmpdir']=dir
  582:         stObj.returnValue['basketLen']=basketLen
  583:         stObj.returnValue['numberOfFiles']=numberOfFiles
  584:         stObj.returnValue['basketNameFromId']=basketNameFromId
  585:         stObj.returnValue['basketNameFromFile']=basketNameFromFile
  586:         stObj.returnValue['basketId']=basketId
  587:         stObj.returnValue['dir']=dir
  588:         #stObj.returnValue['changed']=copy.copy(changed)
  589:         stObj.returnValue['changed']=[(x[0].getId(),x[1][0]) for x in changed]
  590:         #stObj.returnValue['lockerrors']=[x[0].getId() for x in lockerrors]
  591:         stObj.returnValue['lockerrors']=[x for x in lockerrors]
  592:         self.returnValue=True
  593:         #ctx2.cdli_main.setTemp('v_uploadATF_returnValue',True)
  594:     
  595:  
  596: class CDLIBasketContainer(OrderedFolder):
  597:     """contains the baskets"""
  598:     
  599: 
  600:     security=ClassSecurityInfo()
  601:     meta_type="CDLIBasketContainer"
  602:     
  603:     def getPNumbersOfBasket(self,basketName):
  604:         """get all pnumbers of a basket as a list, returns an empty list if basket not found
  605:         @param basketName: name of the basket
  606:         """
  607:         ret=[]
  608:         basketId=self.getBasketIdfromName(basketName)
  609:         if not basketId:
  610:             return []
  611:         
  612:         ob=getattr(self,basketId).getContent()
  613:         
  614:         ret=[x[0].split(".")[0] for x in ob]
  615:         
  616:         return ret
  617:     
  618:     security.declareProtected('manage','getBasketAsOneFile')       
  619:     def getBasketAsOneFile(self,basketName,current="no"):
  620:         """returns all files of the basket combined in one file
  621:         @param basketName: Name of the basket
  622:         @param current: (optional) if current is set to "yes" then the most current version of 
  623:                         all files are downloaded and not the versions of the files as stored in the basket
  624:         """
  625:         ret=""
  626:         basketId=self.getBasketIdfromName(basketName)
  627:         if not basketId:
  628:             return ""
  629:         
  630:         ob=getattr(self,basketId).getLastVersion()
  631:         for object in ob.getContent():
  632:             if current=="no": #version as they are in the basket
  633:                             ret+=str(object[0].getData())+"\n"
  634:             elif current=="yes":
  635:                             #search current object
  636:                             logging.debug("current: %s"%object[1].getId().split(".")[0])
  637:                             founds=self.CDLICatalog.search({'title':object[1].getId().split(".")[0]})
  638:                             if len(founds)>0:      
  639:                                 ret+=str(founds[0].getObject().getLastVersion().getData())+"\n"
  640:         return ret
  641:     
  642:     security.declareProtected('manage','upDateBaskets') 
  643:     def upDateBaskets(self):
  644:         """update content in to objects"""
  645:         
  646:         founds=self.ZopeFind(self,obj_metatypes=['CDLIBasketVersion'],search_sub=1)
  647: 
  648:         for found in founds:
  649:             found[1].updateBasket()
  650:         
  651:     security.declareProtected('manage','deleteBaskets')        
  652:     def deleteBaskets(self,ids=None):
  653:         """delete baskets, i.e. move them into trash folder"""
  654:         
  655:         
  656:         found=self.ZopeFind(self,obj_ids=['trash'])
  657:         
  658:         if len(found)<1:
  659:             manage_addFolder(self, 'trash')
  660:             trash=self._getOb('trash')
  661:         else:
  662:             trash=found[0][1]
  663:         
  664:         if type(ids) is not ListType:
  665:             ids=[ids]
  666:         cut=self.manage_cutObjects(ids)
  667:         trash.manage_pasteObjects(cut)
  668:         
  669:     security.declareProtected('manage','manageBaskets')       
  670:     def manageBaskets(self,submit,ids=None,basket1="",basket2="",joinBasket="",subtractBasket="",REQUEST=None,RESPONSE=None):
  671:         """manage baskets, delete or copy"""
  672:         if submit=="delete":
  673:             self.deleteBaskets(ids)
  674:         
  675:         elif submit=="join":
  676:             flag,msg=self.joinBasket(joinBasket, ids)
  677:             logging.info("joining %s %s"%(flag,msg))
  678:             
  679:         elif submit=="subtract":
  680:             logging.info("BBBb %s %s"%(basket1,basket2))
  681:             flag,msg=self.subtractBasket(subtractBasket, basket1,basket2)
  682:             logging.info("subtract %s %s"%(flag,msg))
  683:             
  684:         if RESPONSE:
  685:             RESPONSE.redirect(self.absolute_url())
  686:     
  687:     security.declareProtected('View','getBasketIdfromName')       
  688:     def getBasketIdfromName(self,basketname):
  689:         """get id from name"""
  690: 
  691:         for basket in self.ZopeFind(self,obj_metatypes=["CDLIBasket"]):
  692:             if basket[1].title==basketname:
  693:                 return basket[0]
  694:         else:
  695:             None
  696:     
  697:     security.declareProtected('manage','uploadBasket_html')        
  698:             
  699:     def uploadBasket_html(self,basketId='0'):
  700:         """upload an atf file, html form"""
  701:         
  702: 
  703:         basketId=str(basketId)
  704:         if not basketId=='0':
  705:             basketName=getattr(self.basketContainer,basketId).title
  706:         else:
  707:             basketName=""
  708:             
  709:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadBasket_html.zpt')).__of__(self)
  710:         return pt(basketId=basketId,basketName=basketName)
  711:    
  712: 
  713:     security.declareProtected('manage','index_html')    
  714:     def index_html(self):
  715:         """stanadard ansicht"""
  716:         
  717: 
  718: 
  719:         ext=self.ZopeFind(self,obj_ids=["index.html"])
  720:         if ext:
  721:             return ext[0][1]()
  722:         
  723:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','BasketContainerMain')).__of__(self)
  724:         return pt()
  725:     
  726:     def getStorageFolderRoot(self):
  727:         """root des storage folders"""
  728:         return self.cdli_main
  729:     
  730:     def __init__(self,id,title):
  731:         """ init basket container"""
  732:         self.id=id
  733:         self.title=title
  734:      
  735:  
  736:     def getBasketsId(self):
  737:         """get all baskets als klartext"""
  738:         
  739:         ret=""
  740:         baskets=self.ZopeFind(self,obj_metatypes=['CDLIBasket'])
  741:         for basket in baskets:
  742:             com,user,time,values = basket[1].getContentIds()
  743:             ret+= "BASKET:"+com+"\t"+user+"\t"+time+"\n"
  744:             for x in values:
  745:                 ret+= x[0]+"\t"+x[1]+"\n"
  746:                 return ret
  747: 
  748:     def getBaskets(self,sortField='title'):
  749:         """get all baskets files"""
  750: 
  751:         def sortName(x,y):
  752:             return cmp(x[1].title.lower(),y[1].title.lower())
  753: 
  754:         def sortDate(x,y):
  755:             return cmp(y[1].getLastVersion().getTime(),x[1].getLastVersion().getTime())
  756: 
  757:         
  758:         def sortComment(x,y):
  759: 
  760:         
  761:             
  762:              try:
  763:                 xc=getattr(x[1],'comment','ZZZZZZZZZZZZZ').lower()
  764:              except:
  765:                 xc='ZZZZZZZZZZZZZ'.lower()
  766:              try:
  767:                 yc=getattr(y[1],'comment','ZZZZZZZZZZZZZ').lower()
  768:              except:
  769:                 yc='ZZZZZZZZZZZZZ'.lower()
  770:     
  771:     
  772:              if (xc=='') or (xc=='ZZZZZZZZZZZZZ'.lower()):
  773:                  
  774:                  try:
  775:                      xc=x[1].getLastVersion().getComment().lower()
  776:                  except:
  777:                      xc='ZZZZZZZZZZZZZ'.lower()
  778:                      
  779:              if (yc=='') or (yc=='ZZZZZZZZZZZZZ'.lower()):
  780:                  try:
  781:                      yc=y[1].getLastVersion().getComment().lower()
  782:                  except:
  783:                      yc='ZZZZZZZZZZZZZ'.lower()
  784:     
  785:              
  786:                  return cmp(xc,yc)
  787:         
  788:         def sortAuthor(x,y):
  789:             
  790:             return cmp(x[1].getLastVersion().getUser().lower(),y[1].getLastVersion().getUser().lower())
  791:         
  792:         baskets=self.ZopeFind(self,obj_metatypes=['CDLIBasket'])
  793:         
  794:         
  795:         if sortField=='title':
  796:             baskets.sort(sortName)
  797:         elif sortField=='date':
  798:             baskets.sort(sortDate)
  799:         elif sortField=='author':
  800:             baskets.sort(sortAuthor)
  801:         elif sortField=='comment':
  802:             baskets.sort(sortComment)
  803: 
  804:         return baskets
  805:     
  806:         
  807:     def subtractBasket(self,newBasket,basket1,basket2):
  808:         """subtract basket2 from basket1 
  809:         (i.e. newbasket will contain alle elements of basket1 which are not in basket2), 
  810:         if basket2 contains files which are not in basket1, then theses files fill be ignored
  811:                
  812:         @param newbasket: name of the new basket
  813:         @param basket1: basket where basket2 will be subtracted from
  814:         @param basket2: see above
  815:       
  816:         """
  817:         logging.info("CCCCC %s %s"%(basket1,basket2))
  818:    
  819:         try:
  820:             newB=self.addBasket(newBasket)
  821:         except:
  822:             return False, "cannot create the new basket"
  823:         
  824:         
  825: 
  826:        
  827:      
  828:         bas2= getattr(self,basket2)            
  829:         bas2content=bas2.getContent()
  830:         bas2ids=[x[0] for x in bas2content]
  831:         
  832:        
  833:             
  834:         bas1= getattr(self,basket1)   
  835:         bas1content=bas1.getContent()
  836:         
  837:         
  838:         newBasketContent={}
  839:         
  840:         for id,version in bas1content:
  841:             if not (id in bas2ids):
  842:                 newBasketContent[id]=version
  843:         
  844:         username=self.getActualUserName()
  845:         
  846:         logging.info("sbc %s"%newBasketContent)
  847:         newB.addObjectsWithVersion(newBasketContent,username=username,catalog=self.CDLICatalog)
  848:         
  849:         return True, ""
  850:     
  851:             
  852:     def joinBasket(self,newBasket,oldBaskets):
  853:         """join two baskets
  854:         @param newbasket: name of the new basket
  855:         @param oldbaskets: list of baskets to be joined
  856:         """
  857:         try:
  858:             newB=self.addBasket(newBasket)
  859:         except:
  860:             return False, "cannot create the new basket"
  861:         
  862:         newBasketContent={}
  863:         for ob in oldBaskets:
  864:             x= getattr(self,ob,None)
  865:             if x is None:
  866:                 return False, "cannot find basket: %s"%ob
  867:             
  868:             ids=x.getContent() # hole den Inhalt
  869:             
  870:             for id,version in ids:
  871:                 if newBasketContent.has_key(id): # p number gibt's schon
  872:                     newBasketContent[id]=max(newBasketContent[id],version) # speichere die groessere Versionsnumber
  873:                 else:
  874:                     newBasketContent[id]=version
  875:         username=self.getActualUserName()
  876:         
  877:         logging.info("nbc %s"%newBasketContent)
  878:         newB.addObjectsWithVersion(newBasketContent,username=username,catalog=self.CDLICatalog)
  879:         
  880:         return True, ""
  881:     
  882:     def getNewId(self):
  883:         """createIds"""
  884:         last=getattr(self,'last',0)
  885:         last +=1
  886:         while len(self.ZopeFind(self,obj_ids=[str(last)]))>0:
  887:             last+=1
  888:     
  889:         self.last=last
  890:         return last
  891:     
  892:     def setActiveBasket(self,basketId,REQUEST=None):
  893:         """store active basketId in a cookie"""
  894:         self.REQUEST.RESPONSE.setCookie("CDLIActiveBasket",basketId,path="/")
  895:         try:
  896:             qs=cgi.parse_qs(REQUEST['QUERY_STRING'])
  897:             del(qs['basketId'])
  898:         except:
  899:             qs={}
  900:         if REQUEST:
  901:             REQUEST.RESPONSE.redirect(REQUEST['URL1']+'?'+urllib.urlencode(qs))
  902:             
  903:     def getActiveBasket(self):
  904:         """get active basket from cookie"""
  905:         
  906:         id= self.REQUEST.cookies.get('CDLIActiveBasket',None)
  907:         if id:
  908:             obj=getattr(self,str(id),None)
  909:         else:
  910:             obj=None
  911:         return obj
  912:     
  913:     def getActualUserName(self):
  914:         """get name of the actualuser"""
  915:         return str(self.REQUEST['AUTHENTICATED_USER'])
  916:     
  917:     security.declareProtected('manage','addBasket') 
  918:     def addBasket(self,newBasketName):
  919:         """add a new basket"""
  920:         
  921:         ob=manage_addCDLIBasket(self,newBasketName)
  922:         return ob
  923: 
  924:     def storeInBasket(self,submit,ids=None,newBasketName=None,fromFileList=None,RESPONSE=None,REQUEST=None):
  925:         """store it"""
  926:         if not ids:
  927:             ids=self.REQUEST.SESSION['fileIds']
  928:            
  929:         if (type(ids) is not ListType) and (not isinstance(ids,Set)):
  930:             ids=[ids]
  931:         
  932:         if isinstance(ids,Set):
  933:             ids=list(ids)
  934:             
  935:         if (submit.lower()=="store in new basket") or (submit.lower()=="new basket"):
  936:             basketRet=self.addBasket(newBasketName)
  937:             self.setActiveBasket(basketRet.getId())
  938:             basket=getattr(self,basketRet.getId())
  939:         elif (submit.lower()=="store in active basket") or (submit.lower()=="active basket"):
  940:             basket=self.getActiveBasket()
  941:         
  942:         added=basket.addObjects(ids)
  943:         back=self.REQUEST['HTTP_REFERER'].split("?")[0]+"?basketName="+basket.title+"&numberOfObjects="+str(added)
  944:         
  945:         
  946:         if fromFileList:
  947: 
  948:             return self.cdli_main.findObjectsFromList(list=ids,basketName=basket.title,numberOfObjects=added)
  949:        
  950:         if RESPONSE:
  951:             
  952:             RESPONSE.redirect(back)
  953:             
  954:         return True
  955:     
  956: def manage_addCDLIBasketContainerForm(self):
  957:     """add the CDLIBasketContainer form"""
  958:     pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addCDLIBasketContainer.zpt')).__of__(self)
  959:     return pt()
  960: 
  961: def manage_addCDLIBasketContainer(self,id,title,RESPONSE=None):
  962:     """add the basket"""
  963:     ob=CDLIBasketContainer(id,title)
  964:     
  965:     self._setObject(id, ob)
  966:     
  967:     if RESPONSE is not None:
  968:         RESPONSE.redirect('manage_main')
  969: 
  970: class CDLIBasket(Folder,CatalogAware):
  971:     """basket"""
  972:     
  973:     meta_type="CDLIBasket"
  974:     default_catalog="CDLIBasketCatalog"
  975:     
  976:     def searchInBasket(self,indexName,searchStr,regExp=False):
  977:         """searchInBasket"""
  978: 
  979:         lst=self.searchInLineIndexDocs(indexName,searchStr,uniq=True,regExp=regExp) #TODO: fix this
  980:         ret={}
  981:         
  982:         lv=self.getLastVersion()
  983: 
  984: 
  985:         for obj in lv.content.getContent():
  986:             id=obj[1].getId().split(".")[0]
  987:             if id in lst:
  988:         
  989:                 ret[id]=self.showWordInFile(id,searchStr,lineList=self.getLinesFromIndex(indexName,searchStr,id,regExp=regExp),regExp=regExp,indexName=indexName)
  990:         
  991:         
  992:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','searchResultsInBasket')).__of__(self)
  993:         return pt(result=ret,indexName=indexName,regExp=regExp,word=searchStr)
  994:         
  995:          
  996:  
  997:         
  998:     def searchInBasket_v1(self,searchStr):
  999:         """search occurences of searchStr in files im basket"""
 1000:         ret=[]
 1001:         lv=self.getLastVersion()
 1002:         logging.info("searching")
 1003:         for obj in lv.content.getContent():
 1004:             txt=obj[0].getData()
 1005:             for x in txt.split("\n"):
 1006:                 logging.info("search %s"%x)
 1007:                 if re.match(searchStr,x):
 1008:                     ret.append(x)
 1009:         
 1010:         return "\n".join(ret)
 1011:                 
 1012:             
 1013:     def getFile(self,obj):
 1014:         return obj[1]
 1015:     
 1016:     def getFileLastVersion(self,obj):
 1017:         return obj[0]
 1018:     
 1019:     def getFileNamesInLastVersion(self):
 1020:         """get content of the last version as list"""
 1021:         
 1022:         return [x[1].getId() for x in self.getLastVersion().getContent()]
 1023:     
 1024: 
 1025:     def isActual(self,obj):
 1026:         """teste ob im basket die aktuelle version ist"""
 1027:         actualNo=obj[1].getLastVersion().getVersionNumber()
 1028:         storedNo=obj[0].getVersionNumber()
 1029:         
 1030:         founds=self.CDLICatalog.search({'title':obj[0].getId()})
 1031:         if len(founds)>0:
 1032:             actualNo=founds[0].getObject().getLastVersion().getVersionNumber()
 1033:             
 1034:         if len(founds)>0 and founds[0].getObject().aq_parent.getId()==".trash":
 1035:             return False, -1
 1036:         
 1037:         if actualNo==storedNo:
 1038:             return True , 0
 1039:         else:
 1040:             return False, actualNo
 1041:         
 1042:     def history(self):
 1043:         """history"""  
 1044: 
 1045:         ext=self.ZopeFind(self.aq_parent,obj_ids=["history_template.html"])
 1046:         if ext:
 1047:             return getattr(self,ext[0][1].getId())()
 1048:         
 1049:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','BasketHistory')).__of__(self)
 1050:         return pt()
 1051:     
 1052:     def getStorageFolderRoot(self):
 1053:         """root des storage folders"""
 1054:         return self.aq_parent.cdli_main
 1055:     
 1056:     def __init__(self,id,title,shortDescription="",comment=""):
 1057:         """init a CDLIBasket"""
 1058:         
 1059:         self.id=id
 1060:         self.title=title
 1061:         self.shortDescription=shortDescription
 1062:         self.comment=comment
 1063:  
 1064:     def getActualUserName(self):
 1065:         """get name of the actualuser"""
 1066:        
 1067:         return str(self.REQUEST['AUTHENTICATED_USER'])
 1068:   
 1069:            
 1070:     def getLastVersion(self):
 1071:         """hole letzte version"""
 1072: 
 1073:         ids=[]
 1074:         idsTmp= self.objectIds()
 1075:         for x in idsTmp:
 1076:             try:
 1077:                 ids.append(int(x))
 1078:             except:
 1079:                 pass
 1080:         ids.sort()
 1081:       
 1082:         if len(ids)==0:
 1083:             return None
 1084:         else:    
 1085:             ob=getattr(self,str(ids[-1]))
 1086: 
 1087:             
 1088:             return ob
 1089:    
 1090:     def getVersions(self):
 1091:         """get versions"""
 1092:         versions=self.ZopeFind(self,obj_metatypes=["CDLIBasketVersion"])
 1093:         return versions
 1094: 
 1095:    
 1096:     def updateObjects(self,ids,RESPONSE=None,REQUEST=None):
 1097:         """update ids, ids not in the basket the add"""
 1098:         if type(ids) is not ListType:
 1099:             ids=[ids]
 1100:        
 1101:         lastVersion=self.getLastVersion() 
 1102:         oldContent=lastVersion.content.getContent()
 1103:         newContent=[]
 1104:         
 1105:         #first copy the old
 1106:         for obj in oldContent:
 1107:             if obj[1].getId() not in ids:
 1108:                 newContent.append(obj)
 1109:         #now add the new
 1110:                
 1111:         for id in ids:
 1112:             founds=self.CDLICatalog.search({'title':id})
 1113: 
 1114:             for found in founds:
 1115:                 if found.getObject() not in oldContent:
 1116:                     #TODO: was passiert wenn, man eine Object dazufŸgt, das schon da ist aber eine neuere version
 1117:                     newContent.append((found.getObject().getLastVersion(),found.getObject()))
 1118:         
 1119: 
 1120:         content=newContent 
 1121:         user=self.getActualUserName()
 1122:         
 1123:         ob=manage_addCDLIBasketVersion(self,user,comment="",basketContent=newContent)
 1124:         
 1125:         obj=self._getOb(ob.getId())
 1126:         if RESPONSE:
 1127:            
 1128:             RESPONSE.redirect(obj.absolute_url())
 1129:         
 1130:         return obj
 1131:     
 1132:     def addObjectsWithVersion(self,ids,deleteOld=None,username=None,catalog=None):
 1133:         """generate a new version of the basket with objects added, 
 1134:         hier wird jedoch nicht die letzte Version jedes Files hinzugefuegt, s
 1135:         ondern ids is ein Tupel mit der Id (d.h. der p-number) und der Versionsnummer.
 1136:         """
 1137:         logging.info("add to basket (%s)"%(self.getId()))
 1138:         lastVersion=self.getLastVersion()
 1139:         
 1140:         if not catalog:
 1141:             catalog=self.CDLICatalog
 1142:             
 1143:         if lastVersion is None:
 1144:             oldContent=[]
 1145:         else:
 1146:             oldContent=lastVersion.content.getContent()
 1147: 
 1148:         if deleteOld:
 1149:             oldContent=[]
 1150: 
 1151:         newContent=[]
 1152:         added=0
 1153:        
 1154:         for id,version in ids.iteritems():
 1155:             logging.info("adding %s %s"%(id,version))
 1156:             id=id.split(".")[0] # title nur die pnumber ohne atf
 1157:            
 1158:             try:
 1159:                 founds=catalog.search({'title':id})
 1160:             except:
 1161:                 founds=[]
 1162:             logging.info(" found %s "%(founds))
 1163:             for found in founds:
 1164:                 if found.getObject() not in oldContent:
 1165:                  
 1166:                     #TODO: was passiert wenn, man eine Object dazufŸgt, das schon da ist aber eine neuere version
 1167:                     newContent.append((found.getObject().getVersions()[version-1][1],found.getObject()))
 1168:                     added+=1
 1169: 
 1170:         content=oldContent+newContent
 1171:         if not username:
 1172:             logging.error("XXXXXXXXXXX %s"%repr(self))
 1173:             user=self.getActualUserName()
 1174:         else:
 1175:             user = username
 1176:             
 1177:         ob=manage_addCDLIBasketVersion(self,user,comment="",basketContent=content)
 1178:         logging.info("add to basket (%s) done"%(self.getId()))
 1179:         return added
 1180:     
 1181:     
 1182:     def addObjects(self,ids,deleteOld=None,username=None):
 1183:         """generate a new version of the basket with objects added"""
 1184:         
 1185:         def swap(x):
 1186:             return (x[1],x[0])
 1187:             
 1188:         logging.info("add to basket (%s)"%(self.getId()))
 1189:         lastVersion=self.getLastVersion()
 1190:         
 1191:         if lastVersion is None:
 1192:             oldContent=[]
 1193:         else:
 1194:             oldContent=lastVersion.content.getContent()
 1195: 
 1196:         if deleteOld:
 1197:             oldContent=[]
 1198: 
 1199:         added=0
 1200: #        for id in ids:
 1201: #            logging.debug("adding:"+id)
 1202: #            try:
 1203: #                founds=self.CDLICatalog.search({'title':id})
 1204: #            except:
 1205: #                founds=[]
 1206: #           
 1207: #            for found in founds:
 1208: #                if found.getObject() not in oldContent:
 1209: #                    #TODO: was passiert wenn, man eine Object dazufŸgt, das schon da ist aber eine neuere version
 1210: #                    newContent.append((found.getObject().getLastVersion(),found.getObject()))
 1211: #                    added+=1
 1212: 
 1213:         hash = md5.new(repr(self.makelist(ids))).digest() # erzeuge hash als identification
 1214:         #logging.debug("JJJJJJJ:"+repr(self.makelist(ids)))
 1215:         logging.debug("JJJJJJJ:"+repr(hash))
 1216:                       
 1217:         if hasattr(self.cdliRoot,'_v_tmpStore') and self.cdliRoot._v_tmpStore.has_key(hash): 
 1218:             logging.debug("from store!")
 1219:             newContent=Set(map(swap,self.cdliRoot._v_tmpStore[hash]))
 1220:          
 1221:         else:
 1222:             logging.debug("not from store!")
 1223:             newContent=Set([(self.getFileObjectLastVersion(x),self.getFileObject(x)) for x in ids])
 1224:         
 1225:         
 1226:         content=Set(oldContent).union(newContent)
 1227:         added = len(content)-len(oldContent)
 1228:         if not username:
 1229:             user=self.getActualUserName()
 1230:         else:
 1231:             user = username
 1232:         
 1233:         #logging.debug("content:"+repr(list(content)))
 1234:         ob=manage_addCDLIBasketVersion(self,user,comment="",basketContent=list(content))
 1235:         logging.info("add to basket (%s) done"%(self.getId()))
 1236:         return added
 1237:     
 1238:     
 1239:                 
 1240:     def getContent(self):
 1241:         """print content"""
 1242:         ret=[]
 1243:         
 1244:         lv=self.getLastVersion()
 1245:         for obj in lv.content.getContent():
 1246:             logging.info("XXXXXXXXXX %s"%repr(obj))
 1247:             ret.append((obj[1].getId(),obj[0].versionNumber))
 1248:             
 1249:         return ret
 1250:         
 1251:     def getContentIds(self):
 1252:         """print basket content"""
 1253:         ret=[]
 1254:         lv=self.getLastVersion()
 1255:         for obj in lv.content.getContent():
 1256:             ret.append((obj[0].getId(),obj[1].getId()))
 1257:         
 1258:         
 1259:         return lv.getComment(),lv.getUser(),lv.getTime(),ret
 1260: 
 1261:     def changeBasket(self,ids,submit,RESPONSE=None,REQUEST=None):
 1262:         """change a basket"""
 1263:         if submit=="update":
 1264:             return self.updateObjects(ids,RESPONSE=RESPONSE,REQUEST=REQUEST)
 1265:         elif submit=="delete":
 1266:             return self.deleteObjects(ids,RESPONSE=RESPONSE,REQUEST=REQUEST)
 1267:             
 1268:     def deleteObjects(self,ids,RESPONSE=None,REQUEST=None):
 1269:         """delete objects"""
 1270:         
 1271:         if type(ids) is not ListType:
 1272:             ids=[ids]
 1273:        
 1274:         lastVersion=self.getLastVersion() 
 1275:         oldContent=lastVersion.content.getContent()
 1276:         newContent=[]
 1277:         for obj in oldContent:
 1278:             if obj[1].getId() not in ids:
 1279:                 newContent.append(obj)
 1280:         
 1281:                 
 1282:         user=self.getActualUserName()
 1283:         
 1284:         ob=manage_addCDLIBasketVersion(self,user,comment="",basketContent=newContent)
 1285:         
 1286:         if RESPONSE:
 1287:             obj=self._getOb(ob.getId())
 1288:             RESPONSE.redirect(obj.absolute_url())
 1289:         
 1290: def manage_addCDLIBasketForm(self):
 1291:     """add the CDLIBasketContainer form"""
 1292:     pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addCDLIBasket.zpt')).__of__(self)
 1293:     return pt()
 1294: 
 1295: def manage_addCDLIBasket(self,title,shortDescription="",comment="",RESPONSE=None):
 1296:     """add the basket"""
 1297:     
 1298:     id=str(self.getNewId())
 1299:     
 1300:     ob=CDLIBasket(id,title,shortDescription,comment)
 1301:     
 1302:     self._setObject(id, ob)
 1303:     
 1304:     if RESPONSE is not None:
 1305:         RESPONSE.redirect('manage_main')
 1306:     else:
 1307:         return ob
 1308: 
 1309: class CDLIBasketVersion(Implicit,Persistent,Folder):
 1310:     """version of a basket"""
 1311:     
 1312:     meta_type="CDLIBasketVersion"
 1313:     security=ClassSecurityInfo()
 1314:     
 1315:     def updateBasket(self):
 1316:         """update"""
 1317:         try:
 1318:             self._setObject('content',BasketContent(self.basketContent))
 1319:         except:
 1320:             try:
 1321:                 if len(self.basketContent)>0:
 1322:                     self.content.setContent(self.basketContent)
 1323:             except:
 1324:                 print "error",self.getId(),self.aq_parent.getId()
 1325:         self.basketContent=[]
 1326: 
 1327:         
 1328:     def containsNonActualFiles(self):
 1329:         """returns True if basket contains one or more non current files"""
 1330:         
 1331:         objs=self.getContent()
 1332:         for obj in objs:
 1333:             if not self.isActual(obj)[0]:
 1334:                 return True
 1335:         return False
 1336:     
 1337:     def downloadListOfPnumbers(self):
 1338:         """download pnumbers of the basket as list"""
 1339:         
 1340:         basket_name=self.aq_parent.title
 1341:         
 1342:         ids=self.getContent() # get the list of objects
 1343:         logging.error(ids)
 1344:         ret="\n".join([x[1].getId().split(".")[0] for x in ids])
 1345:         
 1346:         self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename="%s.txt" """%basket_name)
 1347:         self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream")
 1348:         length=len(ret)
 1349:         self.REQUEST.RESPONSE.setHeader("Content-Length",length)
 1350:         self.REQUEST.RESPONSE.write(ret)    
 1351:         
 1352:     security.declareProtected('manage','downloadObjectsAsOneFile')
 1353:     def downloadObjectsAsOneFile(self,lock=None,procedure=None,REQUEST=None,check="yes",current="no"):
 1354:         """download all selected files in one file"""
 1355:         
 1356:         if self.temp_folder.downloadCounterBaskets > 10000:
 1357:             return """I am sorry, currently the server has to many requests for downloads, please come back later!"""
 1358: 
 1359: 
 1360:         if (check=="yes") and self.containsNonActualFiles():
 1361:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','downloadObjectAsOneFile_check.zpt')).__of__(self)
 1362:             
 1363:             return pt(lock=lock)
 1364:             
 1365:         else:
 1366:             
 1367:             return self.downloadObjectsAsOneFileFinally(lock=lock,procedure=procedure,REQUEST=REQUEST,current="no")
 1368:         
 1369:     def downloadObjectsAsOneFileFinally(self,lock=None,procedure=None,REQUEST=None,current="no"):
 1370:         """print do the download"""
 1371: 
 1372:         ret=""
 1373:         lockedObjects={}
 1374: 
 1375:         self.temp_folder.downloadCounterBaskets+=1 
 1376:         self._p_changed=1
 1377:         transaction.get().commit()       
 1378:     
 1379:         if lock:
 1380:             
 1381:             if str(self.REQUEST['AUTHENTICATED_USER'])=='Anonymous User':
 1382:                 self.temp_folder.downloadCounterBaskets-=1 
 1383:                 self._p_changed=1
 1384:                 transaction.get().commit()      
 1385:                 self.temp_folder.downloadCounterBaskets-=1 
 1386:                 self._p_changed=1
 1387:                 transaction.get().commit()      
 1388:                 return "please login first"
 1389: 
 1390:             #check if a locked object exist in the basket.
 1391:             lockedObjects={}
 1392:             for object in self.content.getContent():
 1393: 
 1394:                 if (not str(object[1].lockedBy)=="") and (not (str(object[1].lockedBy)==str(self.REQUEST['AUTHENTICATED_USER']))):
 1395:                     lockedObjects[object[1].title]=repr(object[1].lockedBy)
 1396:                    
 1397:                     
 1398:             keys=lockedObjects.keys()
 1399:             
 1400:             
 1401:             if len(keys)>0 and (not procedure):
 1402:                 self.REQUEST.SESSION['lockedObjects']=lockedObjects
 1403:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','lockedObjects.zpt')).__of__(self)
 1404:                 
 1405:                 self.temp_folder.downloadCounterBaskets-=1 
 1406:                 self._p_changed=1
 1407:                 transaction.get().commit()      
 1408: 
 1409:                 return pt()
 1410:          
 1411:             elif not procedure: #keine fails gesperrt dann alle donwloaden
 1412:                 procedure="downloadAll" 
 1413:         
 1414:         
 1415: 
 1416: 
 1417:         for object in self.content.getContent():
 1418:                 logging.error("ret:"+repr(object[0]))
 1419:                 logging.error("    -"+repr(procedure))
 1420:                 logging.error("    -"+repr(object[1].lockedBy))
 1421:                 
 1422:                 if (procedure=="downloadAll") or (object[1].lockedBy=='') or (object[1].lockedBy==self.REQUEST['AUTHENTICATED_USER']):
 1423:                     logging.error("ret1")
 1424:                     if current=="no": #version as they are in the basket
 1425:                         logging.error("ret2")
 1426:                         ret+=str(object[0].getData())+"\n"
 1427:                     elif current=="yes":
 1428:                         logging.error("ret3")
 1429:                         #search current object
 1430:                         founds=self.CDLICatalog.search({'title':object[1].getId().split(".")[0]})
 1431:                         if len(founds)>0:      
 1432:                             ret+=str(founds[0].getObject().getLastVersion().getData())+"\n"
 1433:                             
 1434:                 if lock and object[1].lockedBy=='':
 1435:                     object[1].lockedBy=self.REQUEST['AUTHENTICATED_USER']
 1436:         basket_name=self.aq_parent.title+"_V"+self.getId()
 1437:         
 1438:         #write basketname to header of atf file
 1439:         ret="#basket: %s\n"%basket_name+ret
 1440: 
 1441:         self.temp_folder.downloadCounterBaskets-=1 
 1442:         self._p_changed=1
 1443:         transaction.get().commit()      
 1444:         
 1445:         self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename="%s.atf" """%basket_name)
 1446:         self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream")
 1447:         length=len(ret)
 1448:         self.REQUEST.RESPONSE.setHeader("Content-Length",length)
 1449:         self.REQUEST.RESPONSE.write(ret)    
 1450:         return True
 1451:         
 1452:     def numberOfItems(self):
 1453:         """return anzahl der elemente im basket"""
 1454:         return self.content.numberOfItems()
 1455:     
 1456:     def getTime(self):
 1457:         """getTime"""
 1458:         #return self.bobobase_modification_time().ISO()
 1459:       
 1460:         if hasattr(self,'time'):
 1461:             return time.strftime("%Y-%m-%d %H:%M:%S",self.time)
 1462:         elif hasattr(self,'timefixed'):
 1463:             return self.timefixed
 1464:         else:
 1465:             setattr(self,'timefixed',self.bobobase_modification_time().ISO())
 1466:             return self.bobobase_modification_time().ISO()
 1467:     
 1468:     def getContent(self):
 1469:         """get Basket Content"""
 1470:         return self.content.getContent()
 1471: 
 1472:     
 1473:     def __init__(self,id,user,comment="",basketContent=[]):
 1474:         """ init a basket version"""
 1475:         self.id=id
 1476:         self.comment=comment
 1477:         self._setObject('content',BasketContent(basketContent))
 1478:         #self.basketContent=basketContent[0:]a
 1479:         self.user=user
 1480:         self.time=time.localtime()
 1481:         
 1482:     def getUser(self):
 1483:         """get user"""
 1484:         return self.user
 1485:     
 1486:     def getComment(self):
 1487:         """get Comment"""
 1488:         return self.comment
 1489:  
 1490:     security.declareProtected('manage','index_html')
 1491:     def index_html(self):
 1492:             """view the basket"""
 1493: 
 1494:             if self.REQUEST.get('change',False):
 1495:                     ob=self.aq_parent.updateObjects(self.REQUEST['change'])
 1496:                    
 1497:                     self.REQUEST.RESPONSE.redirect(ob.absolute_url())#go to new basket, because changing generates a new basket
 1498:                                         
 1499:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','BasketVersionMain.zpt')).__of__(self)
 1500:             return pt()
 1501:      
 1502:     def getObjUrl(self,result):
 1503:         """getUrl of the version of the object"""
 1504:         objId=result[1].getTitle()
 1505:         founds=self.CDLICatalog.search({'title':objId})
 1506:         if len(founds)>0:
 1507:              return founds[0].getObject().getLastVersion().absolute_url()
 1508:          
 1509:         else: #assume version number
 1510:             splitted=objId.split("_")
 1511:             founds=self.CDLICatalog.search({'title':splitted[1]})        
 1512:             return founds[0].getObject().getLastVersion().absolute_url()+'/'+objId
 1513:    
 1514: def manage_addCDLIBasketVersion(self,user,comment="",basketContent=[],RESPONSE=None):
 1515:     """add a version"""
 1516:     
 1517:     #check for already existing versions
 1518:  
 1519:     lastVersion=self.getLastVersion()
 1520:     if lastVersion is None:
 1521:         newId=str(1)
 1522:     else:
 1523:         newId=str(int(lastVersion.getId())+1)
 1524:     
 1525:     ob=CDLIBasketVersion(newId,user,comment,basketContent)
 1526:     
 1527:     self._setObject(newId, ob)
 1528:     
 1529:     if RESPONSE is not None:
 1530:         RESPONSE.redirect('manage_main')
 1531:     else:
 1532:         return ob
 1533:     
 1534: class CDLIFileObject(CatalogAware,extVersionedFileObject):
 1535:     """CDLI file object"""
 1536:     
 1537:     meta_type="CDLI File Object"
 1538:     default_catalog='CDLIObjectsCatalog'
 1539:     
 1540:     security=ClassSecurityInfo()
 1541:     
 1542:     security.declareProtected('manage','index_html')
 1543: 
 1544:     security.declarePublic('view')
 1545:     view = PageTemplateFile('zpt/viewCDLIFile.zpt', globals())
 1546: 
 1547:     security.declarePublic('editATF')
 1548:     editATF = PageTemplateFile('zpt/editATFFile.zpt', globals())
 1549: 
 1550:     def PrincipiaSearchSource(self):
 1551:            """Return cataloguable key for ourselves."""
 1552:            return str(self)
 1553:        
 1554:     def makeThisVersionCurrent_html(self):
 1555:         """form for making this version current"""
 1556:         
 1557:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','makeThisVersionCurrent.zpt')).__of__(self)
 1558:         return pt()                 
 1559: 
 1560:     security.declarePublic('makeThisVersionCurrent')
 1561:     def makeThisVersionCurrent(self,comment,author,RESPONSE=None):
 1562:         """copy this version to current"""
 1563:         parent=self.aq_parent
 1564:         parent.manage_addVersionedFileObject(id=None,vC=comment,author=author,file=self.getData(),RESPONSE=RESPONSE)
 1565:         #newversion=parent.manage_addCDLIFileObject('',comment,author)
 1566:         #newversion.manage_upload(self.getData())
 1567:                                         
 1568:         #if RESPONSE is not None:
 1569:         #    RESPONSE.redirect(self.aq_parent.absolute_url()+'/history')
 1570: 
 1571:         return True
 1572:     
 1573:     def getFormattedData(self):
 1574:         """fromat text"""
 1575:         data=self.getData()
 1576: #        return re.sub("\s\#lem"," #lem",data) #remove return vor #lem
 1577:         return re.sub("#lem","       #lem",data) #remove return vor #lem
 1578:         
 1579:     
 1580:     security.declarePublic('getPNumber')
 1581:     def getPNumber(self):
 1582:         """get the pnumber"""
 1583:         try:
 1584:                 txt=re.match("&[Pp](\d*)\s*=([^\r\n]*)",self.getData()[0:])
 1585:         except:
 1586:                 txt=self.getData()[0:]
 1587:                 
 1588:                 return "ERROR"
 1589:         try:
 1590:             return "P"+txt.group(1)
 1591:         except:
 1592:             return "ERROR"
 1593: 
 1594:     security.declarePublic('getDesignation')
 1595:     def getDesignation(self):
 1596:         """get the designation out of the file"""
 1597:         try:
 1598:                 txt=re.match("&[Pp](\d*)\s*=([^\r\n]*)",self.getData()[0:])
 1599:         except:
 1600:                 txt=self.getData()[0:]
 1601:                 
 1602:                 return "ERROR"
 1603:         try:
 1604:             return txt.group(2)
 1605:         except:
 1606:             return "ERROR"
 1607: 
 1608:         
 1609: manage_addCDLIFileObjectForm=DTMLFile('dtml/fileAdd', globals(),Kind='CDLIFileObject',kind='CDLIFileObject', version='1')
 1610: 
 1611: def manage_addCDLIFileObject(self,id,vC='',author='', file='',title='',versionNumber=0,
 1612:                              precondition='', content_type='',
 1613:                              from_tmp=False,REQUEST=None):
 1614:     """Add a new File object.
 1615:     Creates a new File object 'id' with the contents of 'file'"""
 1616:  
 1617:     id=str(id)
 1618:     title=str(title)
 1619:     content_type=str(content_type)
 1620:     precondition=str(precondition)
 1621:     
 1622:     id, title = cookId(id, title, file)
 1623: 
 1624:     self=self.this()
 1625: 
 1626:     # First, we create the file without data:
 1627:     self._setObject(id, CDLIFileObject(id,title,versionNumber=versionNumber,versionComment=vC,time=time.localtime(),author=author))
 1628:     fob = self._getOb(id)
 1629:     
 1630:     # Now we "upload" the data.  By doing this in two steps, we
 1631:     # can use a database trick to make the upload more efficient.
 1632: 
 1633:     if file and not from_tmp:
 1634:         fob.manage_upload(file)
 1635:     elif file and from_tmp:
 1636:         fob.manage_file_upload(file) # manage_upload_from_tmp doesn't exist in ExtFile2
 1637:     #    fob.manage_upload_from_tmp(file) # manage_upload_from_tmp doesn't exist in ExtFile2
 1638:     if content_type:
 1639:         fob.content_type=content_type
 1640: 
 1641:     #logging.debug("manage_add: lastversion=%s"%self.getData())
 1642:     logging.debug("reindex1: %s in %s"%(repr(self),repr(self.default_catalog)))
 1643:     self.reindex_object()
 1644:     #logging.debug("manage_add: fob_data=%s"%fob.getData())
 1645:     logging.debug("reindex2: %s in %s"%(repr(fob), repr(fob.default_catalog)))
 1646:     fob.index_object()
 1647: 
 1648:     if REQUEST is not None:
 1649:         REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
 1650:     
 1651: 
 1652: class CDLIFile(extVersionedFile,CatalogAware):
 1653:     """CDLI file"""
 1654:     
 1655:     security=ClassSecurityInfo()
 1656:     meta_type="CDLI file"
 1657:     content_meta_type = ["CDLI File Object"]
 1658:     
 1659:     default_catalog='CDLICatalog'
 1660:     
 1661:     security.declareProtected('manage','index_html')
 1662:     
 1663:     def getLastVersionData(self):
 1664:         """get last version data"""
 1665:         return self.getData()
 1666: 
 1667:     def getLastVersionFormattedData(self):
 1668:         """get last version data"""
 1669:         return self.getContentObject().getFormattedData()
 1670: 
 1671:     def getTextId(self):
 1672:         """returns P-number of text"""
 1673:         # assuming that its the beginning of the title
 1674:         return self.title[:7]
 1675: 
 1676:     #security.declarePublic('history')
 1677:     def history(self):
 1678:         """history"""  
 1679: 
 1680:         ext=self.ZopeFind(self.aq_parent,obj_ids=["history_template.html"])
 1681:         if ext:
 1682:             return getattr(self,ext[0][1].getId())()
 1683:         
 1684:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','versionHistory')).__of__(self)
 1685:         return pt()
 1686: 
 1687: 
 1688:     def getBasketFromId(self,basketid, context=None):
 1689:         """get basket from id"""
 1690: 
 1691:         if not context:
 1692:             context=self
 1693:             
 1694:         for basket in self.ZopeFind(context,obj_metatypes=["CDLIBasket"]):
 1695:             if basket[0]==basketid:
 1696:                 return basket[1]
 1697:         else:
 1698:             None
 1699: 
 1700:  
 1701:     def isContainedInBaskets(self,context=None):
 1702:         """check is this file is part of any basket
 1703:         @param context: (optional) necessessary if CDLIBasketCatalog is not an (inherited) attribute of self, context.CDLIBasketCatalog
 1704:                         has to exist.
 1705:         """
 1706: 
 1707:         if not context:
 1708:             context=self
 1709:         
 1710:         ret=[]
 1711:         for x in context.CDLIBasketCatalog.search({'getFileNamesInLastVersion':self.getId()}):
 1712:             #if the basket x is deleted it seemes to be that x is sometimes still in the Catalog, why?
 1713:             try:
 1714:                 ret.append(x.getObject())
 1715:             except:
 1716:                 pass
 1717:         return ret
 1718:         #return [x.getObject() for x in context.CDLIBasketCatalog.search({'getFileNamesInLastVersion':self.getId()})]
 1719:         
 1720:         
 1721:     def _newContentObject(self, id, title='', versionNumber=0, versionComment=None, time=None, author=None):
 1722:         """factory for content objects. to be overridden in derived classes."""
 1723:         logging.debug("_newContentObject(CDLI)")
 1724:         return CDLIFileObject(id,title,versionNumber=versionNumber,versionComment=versionComment,time=time,author=author)
 1725: 
 1726: 
 1727:     def addCDLIFileObjectForm(self):
 1728:         """add a new version"""
 1729:         
 1730:         if str(self.REQUEST['AUTHENTICATED_USER']) in ["Anonymous User"]:
 1731:             return "please login first"
 1732:         if (self.lockedBy==self.REQUEST['AUTHENTICATED_USER']) or (self.lockedBy==''):
 1733:             out=DTMLFile('dtml/fileAdd', globals(),Kind='CDLIFileObject',kind='CDLIFileObject',version=self.getVersion()).__of__(self)
 1734:             return out()
 1735:         else:
 1736:             return "Sorry file is locked by somebody else"
 1737:         
 1738:     def manage_addCDLIFileObject(self,id,vC,author,
 1739:                                  file='',title='',
 1740:                                  precondition='', 
 1741:                                  content_type='',
 1742:                                  changeName='no',newName='', 
 1743:                                  come_from=None,
 1744:                                  from_tmp=False,RESPONSE=None):
 1745:         """add"""
 1746:       
 1747:         try: #TODO: der ganze vC unsinn muss ueberarbeitet werden
 1748:             vC=self.REQUEST['vC']
 1749:         except:
 1750:             pass
 1751:         
 1752:         ob = self.addContentObject(id, vC, author, file, title, changeName=changeName, newName=newName, from_tmp=from_tmp,
 1753:                                    precondition=precondition, content_type=content_type)
 1754: 
 1755:         try:
 1756:             #FIXME: wozu ist das gut?
 1757:             self.REQUEST.SESSION['objID_parent']=self.getId()
 1758:         except:
 1759:             pass
 1760:   
 1761:         if RESPONSE:
 1762:             if ob.getSize()==0:
 1763:                 self.REQUEST.SESSION['objID']=ob.getId()
 1764:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','errorUploadFile')).__of__(self)
 1765:                 return pt()
 1766:             else:
 1767:                 if come_from and (come_from!=""):
 1768:                     RESPONSE.redirect(come_from+"?change="+self.getId())
 1769:                 else:
 1770:                     RESPONSE.redirect(self.REQUEST['URL2']+'?uploaded=%s'%self.title)
 1771:         else:
 1772:             return ob
 1773:         
 1774:         
 1775: def manage_addCDLIFileForm(self):
 1776:     """interface for adding the OSAS_root"""
 1777:     pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addCDLIFile.zpt')).__of__(self)
 1778:     return pt()
 1779: 
 1780: def manage_addCDLIFile(self,id,title,lockedBy, author=None, RESPONSE=None):
 1781:     """add the OSAS_root"""
 1782:     newObj=CDLIFile(id,title,lockedBy,author)
 1783:                                         
 1784:     tryToggle=True
 1785:     tryCount=0
 1786: 
 1787:     self._setObject(id,newObj)                  
 1788:     getattr(self,id).reindex_object()
 1789:         
 1790:     if RESPONSE is not None:
 1791:         RESPONSE.redirect('manage_main')
 1792: 
 1793: 
 1794: def checkUTF8(data):
 1795:     """check utf 8"""
 1796:     try:
 1797:         data.encode('utf-8')
 1798:         return True
 1799:     except:
 1800:         return False
 1801:     
 1802: 
 1803: def checkFile(filename,data,folder):
 1804:     """check the files"""
 1805:     # first check the file name
 1806:     fn=filename.split(".") # no extension
 1807: 
 1808:     if not fn[0][0]=="P":
 1809:         return False,"P missing in the filename"
 1810:     elif len(fn[0])!=7:
 1811:         return False,"P number has not the right length 6"
 1812:     elif not checkUTF8(data):
 1813:         return False,"not utf-8"
 1814:     else:
 1815:         return True,""
 1816:     
 1817:     
 1818: def splitatf(fh,dir=None,ext=None):
 1819:     """split it"""
 1820:     ret=None
 1821:     nf=None
 1822:     i=0
 1823: 
 1824:     #ROC: why split \n first and then \r???
 1825:     if (type(fh) is StringType) or (type(fh) is UnicodeType):
 1826:         iter=fh.split("\n")
 1827:     else:
 1828:         iter=fh.readlines()
 1829:         
 1830:     for lineTmp in iter:
 1831:         lineTmp=lineTmp.replace(codecs.BOM_UTF8,'') # make sure that all BOM are removed..
 1832:         for line in lineTmp.split("\r"):
 1833:             #logging.log("Deal with: %s"%line)
 1834:             if ext:
 1835:                 i+=1
 1836:                 if (i%100)==0:
 1837:                     ext.result+="."
 1838:                 if i==10000:
 1839:                     i=0
 1840:                     ext.result+="<br>"
 1841:             #check if basket name is in the first line
 1842:             if line.find("#atf basket")>=0: #old convention
 1843:                 ret=line.replace('#atf basket ','')
 1844:                 ret=ret.split('_')[0]
 1845:             elif line.find("#basket:")>=0: #new convention
 1846:                 ret=line.replace('#basket: ','')
 1847:                 ret=ret.split('_')[0]
 1848: 
 1849:             else:
 1850:                 if (len(line.lstrip())>0) and (line.lstrip()[0]=="&"): #newfile
 1851:                     if nf:
 1852:                         nf.close() #close last file
 1853: 
 1854: 
 1855:                     filename=line[1:].split("=")[0].rstrip()+".atf"
 1856:                     if dir:
 1857:                         filename=os.path.join(dir,filename)
 1858:                     nf=file(filename,"w")
 1859:                     logging.info("open %s"%filename)
 1860:                 if nf:    
 1861:                     nf.write(line.replace("\n","")+"\n")
 1862: 
 1863:     try:        
 1864:         nf.close()
 1865:     except:
 1866:         pass
 1867:     
 1868:     if not((type(fh) is StringType) or (type(fh) is UnicodeType)):
 1869:         fh.close()
 1870:     return ret,len(os.listdir(dir))
 1871: 
 1872: 
 1873: class CDLIFileFolder(extVersionedFileFolder):
 1874:     """CDLI File Folder"""
 1875:     
 1876:     security=ClassSecurityInfo()
 1877:     meta_type="CDLI Folder"
 1878:     file_meta_type=['CDLI file']
 1879:     folder_meta_type=['CDLI Folder']
 1880: 
 1881:     file_catalog='CDLICatalog'
 1882: 
 1883:     #downloadCounter=0 # counts how many download for all files currently run, be mehr als 5 wird verweigert.
 1884:     tmpStore2={}
 1885: 
 1886:     def _newVersionedFile(self, id, title='', lockedBy=None, author=None):
 1887:         """factory for versioned files. to be overridden in derived classes."""
 1888:         logging.debug("_newVersionedFile(CDLI)")
 1889:         return CDLIFile(id, title, lockedBy=lockedBy, author=author)
 1890: 
 1891:     def setTemp(self,name,value):
 1892:         """set tmp"""
 1893: 
 1894:         setattr(self,name,value)
 1895:                                         
 1896:     deleteFileForm = PageTemplateFile("zpt/doDeleteFile", globals())
 1897:                                        
 1898:     def delete(self,ids,REQUEST=None):
 1899:         """delete these files"""
 1900:         if type(ids) is not ListType:
 1901:             ids=[ids]
 1902: 
 1903:         self.manage_delObjects(ids)
 1904:         
 1905:         if REQUEST is not None:
 1906:             return self.index_html()
 1907: 
 1908: 
 1909:     def getVersionNumbersFromIds(self,ids):
 1910:         """get the numbers of the current versions of documents described by their ids"""
 1911:         
 1912:         ret=[]
 1913:         searchStr=" OR ".join(ids)
 1914:         
 1915:         founds=self.CDLICatalog.search({'title':searchStr})
 1916:         
 1917:         for found in founds:
 1918:             lastVersion=found.getObject().getContentObject()
 1919:             ret.append((found.getId,lastVersion))
 1920:         
 1921:         return ret
 1922:     
 1923:     def getFile(self,fn):
 1924:         """get the content of the file fn"""
 1925:         logging.debug("getFile: %s"%repr(fn))
 1926:         if not self.hasObject(fn):
 1927:             # search deeper
 1928:             founds=getattr(self, self.file_catalog).search({'textid':fn})
 1929:             if founds:
 1930:                 obj=founds[0].getObject().getContentObject()
 1931:             else:
 1932:                 return "" 
 1933:         else:
 1934:             obj = self[fn].getContentObject()
 1935: 
 1936:         return obj.getData()[0:] 
 1937:  
 1938:     
 1939:     def checkCatalog(self,fn):
 1940:         """check if fn is in the catalog"""
 1941:         #TODO add checkCatalog
 1942:         
 1943:                                    
 1944:     def findObjectsFromListWithVersion(self,list,author=None):
 1945:         """find objects from a list with versions
 1946:         @param list: list of tuples  (cdliFile,version)
 1947:         """
 1948:         #self.REQUEST.SESSION['fileIds']=list#store fieldIds in session for further usage
 1949:         #self.REQUEST.SESSION['searchList']=self.REQUEST.SESSION['fileIds']
 1950:         
 1951:         pt=getattr(self,'filelistVersioned.html')
 1952:             
 1953:         return pt(search=list,author=author)
 1954:     
 1955:     
 1956:     def getAllPNumbers(self):
 1957:         """get a list of all files (resp their p-numbers) stored"""
 1958:         
 1959:         ret=[x.getId for x in  self.CDLICatalog()]
 1960:      
 1961:         return ret
 1962:     
 1963:     def expandFile(self,fileId,fileTree):
 1964:         """wildcard in fileID suche alle Treffer"""
 1965:         founds=self.CDLICatalog({'title':fileId})
 1966:         for found in founds:
 1967:             fileTree.add(found.getId)
 1968:             logging.debug("ADDD:"+found.getId)
 1969:          
 1970:     def findObjectsFromList(self,enterList=None,display=False,start=None,upload=None,list=None,basketName=None,numberOfObjects=None,RESPONSE=None,REQUEST=None):
 1971:         """findObjectsFromList (, TAB oder LINE separated)"""
 1972:                                        
 1973:         logging.debug("start: findObjectsFromList")
 1974:         if upload: # list from file upload
 1975:             txt=upload.read()
 1976:                                        
 1977:         if enterList:
 1978:             txt=enterList
 1979:             
 1980:         if upload or enterList:
 1981:             txt=txt.replace(",","\n")
 1982:             txt=txt.replace("\t","\n")
 1983:             txt=txt.replace("\r","\n")
 1984:             idsTmp=txt.split("\n")
 1985:             ids=[]
 1986:             for id in idsTmp: # make sure that no empty lines
 1987:                 idTmp=id.lstrip().rstrip()
 1988:                 if len(idTmp)>0:
 1989:                     
 1990:                     ids.append(idTmp)
 1991:                     
 1992:             #self.REQUEST.SESSION['ids']=" OR ".join(ids)
 1993: 
 1994:             pt=getattr(self,'filelist.html')
 1995:             self.REQUEST.SESSION['searchList']=ids
 1996:             return pt(search=ids)
 1997:         
 1998:         if basketName:
 1999:             #TODO: get rid of one of these..
 2000:             
 2001:             pt=getattr(self,'filelist.html')
 2002:             return pt(basketName=basketName,numberOfObjects=numberOfObjects)
 2003:         
 2004:         if list is not None: # got already a list
 2005:             
 2006:             logging.debug(" ----List version")
 2007:             ret=[]
 2008:             fileTree=Set()
 2009:             
 2010:             for fileId in list:
 2011:                
 2012:                 if fileId.find("*")>-1: #check for wildcards
 2013:                         self.expandFile(fileId,fileTree)
 2014:                         
 2015:                 elif len(fileId.split("."))==1:
 2016:                         fileId=fileId+".atf"
 2017:                         fileTree.add(fileId)
 2018:                 #logging.debug("   -----:"+fileId)
 2019:                 #ret+=self.CDLICatalog({'title':fileId})
 2020:                 #x =self.getFileObject(fileId)
 2021:                 #if x is not None:
 2022:                 #    ret.append(x)
 2023:                 
 2024:             
 2025:             
 2026:             ids = fileTree & self.v_file_ids
 2027:             
 2028:             
 2029:             hash = md5.new(repr(self.makelist(ids))).digest() # erzeuge hash als identification
 2030:             #TODO: do I need garbage collection for _v_tmpStore ?
 2031:             #logging.debug("list:"+repr(self.makelist(ids)))
 2032:             #logging.debug("Hash:"+repr(hash))
 2033:             if hasattr(self.cdliRoot,'_v_tmpStore') and self.cdliRoot._v_tmpStore.has_key(hash): 
 2034:                logging.debug("asking for storage")
 2035:                return self.cdliRoot._v_tmpStore[hash]
 2036:           
 2037:             #TODO: get rid of one of these..
 2038:             #ids=[x.getObject().getId() for x in ret]
 2039:             ret=[(self.getFileObject(x),self.getFileObjectLastVersion(x)) for x in ids]
 2040:             
 2041:             #self.REQUEST.SESSION['fileIds']=ids#store fieldIds in session for further usage
 2042:             #self.REQUEST.SESSION['searchList']=self.REQUEST.SESSION['fileIds']
 2043:             self.REQUEST.SESSION['fileIds']=list#store fieldIds in session for further usage
 2044:             self.REQUEST.SESSION['searchList']=self.REQUEST.SESSION['fileIds']
 2045:          
 2046:             if display:
 2047:                 pt=getattr(self,'filelist.html')
 2048:                 
 2049:                 return pt(search=ids)
 2050:             else:     
 2051:                 #self.REQUEST.SESSION['hash'] = ret # store in session 
 2052:                 if not hasattr(self,'_v_tmpStore'):
 2053:                     self.cdliRoot._v_tmpStore={}
 2054:                 #logging.debug("HHHHHHNEU:"+repr(self.makelist(ids)))
 2055:                 #logging.debug("HHHHHHNEU:"+repr(hash))
 2056:                 self.cdliRoot._v_tmpStore[hash] = ret # store in session 
 2057:                 return ret
 2058:         
 2059:         
 2060:         
 2061:         if start:
 2062:             RESPONSE.redirect("filelist.html?start:int="+str(start))
 2063:                                        
 2064:     def makelist(self,mySet):
 2065:         x = list(mySet)
 2066:         x.sort()
 2067:         return x
 2068:     
 2069:     security.declareProtected('Manage','createAllFilesAsSingleFile')
 2070:     def createAllFilesAsSingleFile(self,RESPONSE=None):
 2071:         """download all files"""
 2072:         
 2073:         def sortF(x,y):
 2074:             return cmp(x[0],y[0])
 2075:         
 2076:         catalog=getattr(self,self.file_catalog)
 2077:         #tf,tfilename=mkstemp()
 2078:         if not hasattr(self.temp_folder,'downloadCounter'):
 2079:             self.temp_folder.downloadCounter=0
 2080: 
 2081:         if getattr(self.temp_folder,'downloadCounter',0) > 5:
 2082:             return """I am sorry, currently the server has to many requests for downloads, please come back later!"""
 2083: 
 2084:         self.temp_folder.downloadCounter+=1
 2085:         self._p_changed=1
 2086:         transaction.get().commit()
 2087:        
 2088:         list=[(x.getId,x) for x in catalog()]
 2089:         list.sort(sortF)
 2090:         
 2091: 
 2092:         
 2093:         RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%"all.atf")
 2094:         RESPONSE.setHeader("Content-Type","application/octet-stream")
 2095:         tmp=""
 2096:         for l in list:
 2097:             obj=l[1].getObject()
 2098:             
 2099:             if obj.meta_type=="CDLI file":
 2100:                 
 2101:                 #os.write(tf,obj.getLastVersion().data)
 2102:                 if RESPONSE:
 2103:                     RESPONSE.write(obj.getData()[0:])
 2104:                     RESPONSE.write("\n")
 2105:                 self.temp_folder.downloadCounter-=1 
 2106:                 self._p_changed=1
 2107:         transaction.get().commit()
 2108:         #os.close(tf)
 2109:         #RESPONSE.redirect(self.absolute_url()+"/downloadFile?fn="%tfilename)
 2110:         return True
 2111:     
 2112:     def downloadFile(self,fn):
 2113:         """download fn - not used yet"""
 2114:         self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%self.getLastVersion().getId())
 2115:         self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream")
 2116:         self.REQUEST.RESPONSE.write(file(fn).read())
 2117:         
 2118:       
 2119:                 
 2120:     def hasParent(self):
 2121:         """returns true falls subfolder"""
 2122:       
 2123:         if self.aq_parent.meta_type in self.folder_meta_type:
 2124:             return True
 2125:         else:
 2126:             return False
 2127:         
 2128:     def getFolders(self):
 2129:         """get all subfolders"""
 2130:         ret=[]
 2131:         folders=self.ZopeFind(self,obj_metatypes=self.folder_meta_type)
 2132:         for folder in folders:
 2133:             ret.append((folder[1],
 2134:                         len(self.ZopeFind(folder[1],obj_metatypes=self.folder_meta_type)),
 2135:                         len(self.ZopeFind(folder[1],obj_metatypes=self.file_meta_type))
 2136:                         ))
 2137:         return ret
 2138:     
 2139:             
 2140:     security.declareProtected('manage','index_html')
 2141:     def index_html(self):
 2142:         """main"""
 2143:         ext=self.ZopeFind(self,obj_ids=["index.html"])
 2144:         if ext:
 2145:             return ext[0][1]()
 2146:         
 2147:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','CDLIFileFolderMain')).__of__(self)
 2148:         return pt()
 2149:     
 2150:     
 2151: manage_addCDLIFileFolderForm=DTMLFile('dtml/folderAdd', globals())
 2152: 
 2153:     
 2154: def manage_addCDLIFileFolder(self, id, title='',
 2155:                      createPublic=0,
 2156:                      createUserF=0,
 2157:                      REQUEST=None):
 2158:     """Add a new Folder object with id *id*.
 2159: 
 2160:     If the 'createPublic' and 'createUserF' parameters are set to any true
 2161:     value, an 'index_html' and a 'UserFolder' objects are created respectively
 2162:     in the new folder.
 2163:     """
 2164:     ob=CDLIFileFolder()
 2165:     ob.id=str(id)
 2166:     ob.title=title
 2167:     self._setObject(id, ob)
 2168:     ob=self._getOb(id)
 2169: 
 2170:     checkPermission=getSecurityManager().checkPermission
 2171: 
 2172:     if createUserF:
 2173:         if not checkPermission('Add User Folders', ob):
 2174:             raise Unauthorized, (
 2175:                   'You are not authorized to add User Folders.'
 2176:                   )
 2177:         ob.manage_addUserFolder()
 2178: 
 2179:   
 2180:     if REQUEST is not None:
 2181:         return self.manage_main(self, REQUEST, update_menu=1)
 2182:     
 2183: class CDLIRoot(Folder):
 2184:     """main folder for cdli"""
 2185:     
 2186:     meta_type="CDLIRoot"
 2187:     downloadCounterBaskets=0 # counts the current basket downloads if counter > 10 no downloads are possible
 2188:     
 2189:     file_catalog = 'CDLICatalog'
 2190:     
 2191:     # word splitter for search
 2192:     splitter = {'words':cdliSplitter.wordSplitter(),
 2193:                 'graphemes':cdliSplitter.graphemeSplitter()}
 2194:     
 2195:     
 2196:     def getFileObject(self,fileId):
 2197:         x=self.v_files.get(fileId)
 2198:         #logging.debug(x)
 2199:         return x
 2200:     
 2201:     def getFileObjectLastVersion(self,fileId):
 2202:         x=self.v_files_lastVersion.get(fileId)
 2203:         #logging.debug(x)
 2204:         return x
 2205:     
 2206:     def generateFileBTree(self):
 2207:         """erzeuge einen Btree aus allen Files"""
 2208:         self.v_files = OOBTree()
 2209:         self.v_files_lastVersion = OOBTree()
 2210:         self.v_file_ids = Set()
 2211:         
 2212:         for x in self.CDLICatalog.searchResults():
 2213:             
 2214:             self.v_files.update({x.getId:x.getObject()})
 2215:             self.v_files_lastVersion.update({x.getId:x.getObject().getLastVersion()})
 2216:             self.v_file_ids.add(x.getId)
 2217:             logging.debug("add:"+x.getId+"XXX"+repr(x.getObject()))
 2218:         
 2219:         return "done"
 2220:     def deleteFiles(self,ids):
 2221:         """delete files"""
 2222:         for id in ids:
 2223:             founds=self.CDLICatalog.search({'title':id.split(".")[0]})
 2224:             if founds:
 2225:                 logging.debug("deleting %s"%founds)
 2226:                 folder=founds[0].getObject().aq_parent #get the parent folder of the object
 2227:                 logging.debug("deleting from %s"%folder)
 2228:                 cut=folder.delete([founds[0].getId]) #cut it out
 2229: 
 2230: 
 2231: 
 2232:     def searchText(self, query, index='graphemes'):
 2233:         """searches query in the fulltext index and returns a list of file ids/P-numbers"""
 2234:         # see also: http://www.plope.com/Books/2_7Edition/SearchingZCatalog.stx#2-13
 2235:         logging.debug("searchtext for '%s' in index %s"%(query,index))
 2236:         #import Products.ZCTextIndex.QueryParser
 2237:         #qp = QueryParser.QueryParser()
 2238:         #logging.debug()
 2239:         idxQuery = {index:{'query':query}}
 2240:         idx = getattr(self, self.file_catalog)
 2241:         # do search
 2242:         resultset = idx.search(query_request=idxQuery,sort_index='textid')
 2243:         # put only the P-Number in the result 
 2244:         results = [res.getId[:7] for res in resultset]
 2245:         logging.debug("searchtext: found %d texts"%len(results))
 2246:         return results
 2247: 
 2248: 
 2249:     def getFile(self, pnum):
 2250:         """get the translit file with the given pnum"""
 2251:         f = getattr(self, self.file_catalog).search({'textid':pnum})
 2252:         if not f:
 2253:             return ""
 2254:         
 2255:         return f[0].getObject().getData()
 2256:          
 2257: 
 2258:     def showFile(self,fileId,wholePage=False):
 2259:         """show a file
 2260:         @param fileId: P-Number of the document to be displayed
 2261:         """
 2262:         f=getattr(self, self.file_catalog).search({'textid':fileId})
 2263:         if not f:
 2264:             return ""
 2265:         
 2266:         if wholePage:
 2267:             logging.debug("show whole page")
 2268:             return f[0].getObject().getContentObject().view()
 2269:         else:
 2270:             return f[0].getObject().getLastVersionFormattedData()
 2271:     
 2272: 
 2273:     def showWordInFile(self,fileId,word,indexName='graphemes',regExp=False,):
 2274:         """get lines with word from FileId"""
 2275:         logging.debug("showwordinfile word='%s' index=%s file=%s"%(word,indexName,fileId)) 
 2276:         
 2277:         file = formatAtfFullLineNum(self.getFile(fileId))
 2278:         ret=[]
 2279:         
 2280:         # add whitespace before and whitespace and line-end to splitter bounds expressions
 2281:         bounds = self.splitter[indexName].bounds
 2282:         splitexp = "(%s|\s)(%%s)(%s|\s|\Z)"%(bounds,bounds)
 2283:         # clean word expression 
 2284:         # TODO: this should use QueryParser itself
 2285:         # take out double quotes
 2286:         word = word.replace('"','')
 2287:         # take out ignorable signs
 2288:         ignorable = self.splitter[indexName].ignorex
 2289:         word = ignorable.sub('', word)
 2290:         # compile into regexp objects and escape parens
 2291:         wordlist = [re.compile(splitexp%re.escape(w)) for w in word.split(' ')]
 2292:             
 2293:         for line in file.splitlines():
 2294:             for word in wordlist:
 2295:                 #logging.debug("showwordinfile: searching for %s in %s"%(word.pattern,ignoreable.sub('',line)))
 2296:                 if word.search(ignorable.sub('',line)):
 2297:                     line = formatAtfLineHtml(line)
 2298:                     ret.append(line)
 2299:                     break
 2300:                     
 2301:         return ret
 2302: 
 2303:     
 2304:     def showWordInFiles(self,fileIds,word,indexName='graphemes',regExp=False):
 2305:         """
 2306:         get lines with word from all ids in list FileIds.
 2307:         returns dict with id:lines pairs.
 2308:         """
 2309:         logging.debug("showwordinfiles word='%s' index=%s file=%s"%(word,indexName,fileIds))
 2310:         
 2311:         return dict([(id,self.showWordInFile(id, word, indexName, regExp)) for id in fileIds])
 2312:     
 2313: 
 2314:     def tagWordInFile(self,fileId,word,indexName='graphemes',regExp=False):
 2315:         """get text with word highlighted from FileId"""
 2316:         logging.debug("tagwordinfile word='%s' index=%s file=%s"%(word,indexName,fileId)) 
 2317:         
 2318:         file=self.getFile(fileId)
 2319:         tagStart=u'<span class="found">'
 2320:         tagEnd=u'</span>'
 2321:         tagStr=tagStart + u'%%s' + tagEnd
 2322:         ret=[]
 2323:         
 2324:         # add whitespace to splitter bounds expressions and compile into regexp object
 2325:         bounds = self.splitter[indexName].bounds
 2326:         wordsplit = re.compile("(%s|\s)"%bounds)
 2327:         # clean word expression 
 2328:         # TODO: this should use QueryParser itself
 2329:         word = word.replace('"','') # take out double quotes
 2330:         # take out ignoreable signs
 2331:         ignorable = self.splitter[indexName].ignorex
 2332:         word = ignorable.sub('', word)
 2333:         # split search terms by blanks
 2334:         words = word.split(' ')
 2335:         # split search terms again (for grapheme search with words)
 2336:         splitwords = dict(((w,self.splitter[indexName].process([w])) for w in words))
 2337:             
 2338:         for line in file.splitlines():
 2339:             line = unicodify(line)
 2340:             # ignore lemma and other lines
 2341:             if line.lstrip().startswith('#lem:'):
 2342:                 continue
 2343:             # ignore p-num line
 2344:             if line.startswith('&P'):
 2345:                 continue
 2346:             # ignore version lines
 2347:             if line.startswith('#version'):
 2348:                 continue
 2349:             # ignore atf type lines
 2350:             if line.startswith('#atf:'):
 2351:                 continue
 2352: 
 2353:             # first scan
 2354:             hitwords = []
 2355:             for w in words:
 2356:                 if ignorable.sub('',line).find(w) > -1:
 2357:                     # word is in line
 2358:                     # append split word for grapheme search with words
 2359:                     hitwords.extend(splitwords[w])
 2360:                     #hitwords.extend(wordsplit.split(w))
 2361:                    
 2362:             # examine hits closer
 2363:             if hitwords:
 2364:                 # split line into words
 2365:                 parts = wordsplit.split(line)
 2366:                 line = ""
 2367:                 for p in parts:
 2368:                     #logging.debug("tagwordinfile: searching for %s in %s"%(p,hitwords))
 2369:                     # reassemble line
 2370:                     if ignorable.sub('', p) in hitwords:
 2371:                         #logging.debug("tagwordinfile: found %s in %s"%(p,hitwords))
 2372:                         # this part was found
 2373:                         line += tagStart + formatAtfHtml(p) + tagEnd
 2374:                     else:
 2375:                         line += formatAtfHtml(p)
 2376:                 
 2377:             else:
 2378:                 # no hits
 2379:                 line = formatAtfHtml(line)
 2380:             
 2381:             ret.append(line)
 2382:                         
 2383:         return u'<br>\n'.join(ret)
 2384: 
 2385: 
 2386: 
 2387:     def tagWordInFiles(self,fileIds,word,indexName='graphemes',regExp=False):
 2388:         """
 2389:         get texts with highlighted word from all ids in list FileIds.
 2390:         returns dict with id:text pairs.
 2391:         """
 2392:         logging.debug("tagwordinfiles word='%s' index=%s file=%s"%(word,indexName,fileIds)) 
 2393:         return dict([(id,self.tagWordInFile(id, word, indexName, regExp)) for id in fileIds])
 2394:     
 2395: 
 2396:     def getFileVersionList(self, pnum):
 2397:         """get the version history as a list for the translit file with the given pnum"""
 2398:         f = getattr(self, self.file_catalog).search({'textid':pnum})
 2399:         if not f:
 2400:             return []
 2401:         
 2402:         return f[0].getObject().getVersionList()
 2403:          
 2404: 
 2405:     def URLquote(self,str):
 2406:         """quote url"""
 2407:         return urllib.quote(str)
 2408:     
 2409:     def URLunquote(self,str):
 2410:         """unquote url"""
 2411:         return urllib.unquote(str)
 2412:     
 2413:     def URLquote_plus(self,str):
 2414:         """quote url"""
 2415:         return urllib.quote_plus(str)
 2416:     
 2417:     def URLunquote_plus(self,str):
 2418:         """unquote url"""
 2419:         return urllib.unquote_plus(str)
 2420:     
 2421:     
 2422:     def forceunlock(self):
 2423:         "break all locks"
 2424:         ret=[]
 2425:         for f in self.ZopeFind(self,obj_metatypes="CDLI file",search_sub=1):
 2426:            un=f[1].forceunlock()
 2427: 
 2428:            if un and un !="":
 2429:                ret.append((f[0],un))
 2430: 
 2431:         return ret
 2432:                                         
 2433: 
 2434:     def getChangesByAuthor(self,author,n=100):
 2435:         """getChangesByAuthor"""
 2436:         zcat=self.CDLIObjectsCatalog
 2437:         res=zcat({'lastEditor':author,
 2438:                      'sort_on':'getTime',
 2439:                      'sort_order':'descending',
 2440:                      'sort_limit':n})[:n ]
 2441:                        
 2442:         return res
 2443:     
 2444:     def getChangesByAuthor_html(self,author,n=100):
 2445:         """html output for changes by author"""
 2446:         tmp={}
 2447:         list=[]                         
 2448:         for x in self.getChangesByAuthor(author):
 2449:            nr=x.getObject().getVersionNumber()
 2450:            id=x.getObject().aq_parent.getId()
 2451:            #hinzufuegen, wenn Version neuer als die 
 2452:            if tmp.get(id,(0,0))[1] < nr:
 2453:                 tmp[id]=(x.getObject().aq_parent,nr)
 2454: 
 2455:      
 2456:         return self.cdli_main.findObjectsFromListWithVersion(list=tmp.values(),author=author)           
 2457:         
 2458:     def getLastChanges(self,n=100):
 2459:         """get the last n changes""" 
 2460:         n=int(n)                   
 2461:         zcat=self.CDLICatalog
 2462:         return zcat({'sort_on':'getLastChangeDate',
 2463:                      'sort_order':'descending',
 2464:                      'sort_limit':n})[:n ]
 2465:      
 2466:     
 2467:     def getLastChanges_html(self,n=100):
 2468:         """get the last n changes"""
 2469:         list = [x.getId for x in self.getLastChanges(n)]
 2470:         return self.cdli_main.findObjectsFromList(list=list,display=True)
 2471:                                        
 2472:     def refreshTxt(self,txt="",threadName=None):
 2473:         """txt fuer refresh"""
 2474:   
 2475:         return """ 2;url=%s?repeat=%s """%(self.absolute_url()+txt,threadName)
 2476: 
 2477:     
 2478:     def getResult(self,threadName=None):
 2479:        """result of thread"""
 2480:        try:
 2481:         return self._v_uploadATF[threadName].getResult()
 2482:        except:
 2483:         return "One moment, please"
 2484:     
 2485:         
 2486:     def checkThreads(self):
 2487:         """check threads"""
 2488:         ret="<html><body>"
 2489:         for thread in threading.enumerate():
 2490:            ret+="<p>%s (%s): %s</p>"%(repr(thread),thread.getName(),thread.isAlive())
 2491:        
 2492:         return ret
 2493:                                        
 2494:                                            
 2495:     def uploadATFRPC(self,data,username):
 2496:         """upload an atffile via xml-rpc"""
 2497:         uploader=uploadATFThread()
 2498:         
 2499:         #generate an random id for the upload object
 2500:         from random import randint
 2501:         if (not self.REQUEST.SESSION.get('idTmp',None)):
 2502: 
 2503:             idTmp=str(randint(0,1000000000))
 2504:             self.REQUEST.SESSION['idTmp']=idTmp
 2505:         else:
 2506:             idTmp=self.REQUEST.SESSION.get('idTmp',None)
 2507:             
 2508:         
 2509:         uploader.set(data,0,username,idTmp)
 2510:         
 2511:         stObj=uploader.run()
 2512:         
 2513:         processor=uploadATFfinallyThread()
 2514:         
 2515:         basketname=stObj.returnValue['basketNameFromFile']
 2516:         
 2517:         processor.set("uploadchanged",basketname=basketname,SESSION=stObj.returnValue,username=username,serverport=self.REQUEST['SERVER_PORT'])
 2518:         
 2519:         processor.run()
 2520:         
 2521:         
 2522:         return generateXMLReturn(stObj.returnValue)
 2523:         
 2524:     def uploadATF(self,repeat=None,upload=None,basketId=0,RESPONSE=None):
 2525:         """upload an atf file / basket file"""
 2526:         #self._v_uploadATF.returnValue=None
 2527:         
 2528:         #generate an random id for the upload thread
 2529:         from random import randint
 2530:         if (not self.REQUEST.SESSION.get('idTmp',None)):
 2531: 
 2532:             idTmp=str(randint(0,1000000000))
 2533:             self.REQUEST.SESSION['idTmp']=idTmp
 2534:         else:
 2535:             idTmp=self.REQUEST.SESSION.get('idTmp',None)
 2536:             
 2537:     
 2538:         threadName=repeat
 2539:         if not threadName or threadName=="":
 2540:             #new thread not called from the waiting page
 2541:             tmpVar=False
 2542:        
 2543:             thread=uploadATFThread()
 2544:             threadName=thread.getName()[0:]                                
 2545:             if (not hasattr(self,'_v_uploadATF')):
 2546:                    self._v_uploadATF={}
 2547:                                        
 2548:             self._v_uploadATF[threadName]=thread
 2549:             #self._xmltrans.start()
 2550:             #thread=Thread(target=self._v_uploadATF)
 2551:             logging.info("set thread. extern")
 2552:             self._v_uploadATF[threadName].set(upload,basketId,self.REQUEST['AUTHENTICATED_USER'],idTmp,serverport=self.REQUEST['SERVER_PORT'])
 2553:             #thread.start()
 2554:             logging.info("start thread. extern")
 2555:             self._v_uploadATF[threadName].start()
 2556: 
 2557:             
 2558:             self.threadName=self._v_uploadATF[threadName].getName()[0:]
 2559:             wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
 2560: 
 2561:             if wait_template:
 2562:                 return wait_template[0][1]()
 2563:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
 2564:             return pt(txt='/uploadATF',threadName=threadName)
 2565:             #_v_xmltrans.run()
 2566:             
 2567:         else:
 2568:             #recover thread, if lost
 2569:             if (not hasattr(self,'_v_uploadATF')):
 2570:                self._v_uploadATF={}
 2571:             if not self._v_uploadATF.get(threadName,None):
 2572:                  for thread in threading.enumerate():
 2573:                          if threadName == thread.getName():
 2574:                                        self._v_uploadATF[threadName]=thread
 2575:                                        
 2576:             if self._v_uploadATF.get(threadName,None) and (not self._v_uploadATF[threadName].returnValue):
 2577:         
 2578: 
 2579:                 wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
 2580:                 if wait_template:
 2581:                         return wait_template[0][1]()
 2582:                 
 2583:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
 2584: 
 2585:                 return pt(txt='/uploadATF',threadName=threadName)
 2586:                 
 2587:             else:
 2588:                 tmp=getattr(self.temp_folder,idTmp).returnValue
 2589:  
 2590:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadCheck.zpt')).__of__(self)
 2591: 
 2592:                 return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'],
 2593:                   basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId'])
 2594:                      
 2595:     def redoUpload(self,threadName):
 2596:        """redo the upload"""
 2597:        tmp=self.cdli_main.tmpStore2[threadName]
 2598:        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadCheck.zpt')).__of__(self)
 2599:        return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'],
 2600:                   basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId'])
 2601:                  
 2602:     def uploadATFfinally(self,procedure='',comment="",basketname='',unlock=None,repeat=None,RESPONSE=None):
 2603:         """nowupload the files"""
 2604:        
 2605:        
 2606:        
 2607:         threadName=repeat
 2608:         if not threadName or threadName=="":
 2609:             thread=uploadATFfinallyThread()
 2610:             threadName=thread.getName()[0:]
 2611: 
 2612:             if (not hasattr(self,'_v_uploadATF')):
 2613:                                 self._v_uploadATF={}
 2614: 
 2615: 
 2616:             self._v_uploadATF[threadName]=thread
 2617: 
 2618:             idTmp=self.REQUEST.SESSION['idTmp']
 2619:             stObj=getattr(self.temp_folder,idTmp)
 2620:             self._v_uploadATF[threadName].set(procedure,comment=comment,basketname=basketname,unlock=unlock,SESSION=stObj.returnValue,username=self.REQUEST['AUTHENTICATED_USER'],serverport=self.REQUEST['SERVER_PORT'])
 2621: 
 2622:             self._v_uploadATF[threadName].start()
 2623: 
 2624:             
 2625:             
 2626:             wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
 2627: 
 2628:             if wait_template:
 2629:                 return wait_template[0][1]()
 2630:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
 2631: 
 2632:             return pt(txt='/uploadATFfinally',threadName=threadName)
 2633:             #_v_xmltrans.run()
 2634:         
 2635:         else:
 2636:             #recover thread, if lost
 2637:             if not hasattr(self,'_v_uploadATF'):
 2638:                self._v_uploadATF={}
 2639:             if not self._v_uploadATF.get(threadName,None):
 2640:                  for thread in threading.enumerate():
 2641:                          if threadName == thread.getName():
 2642:                                        self._v_uploadATF[threadName]=thread
 2643:                                        
 2644:             if self._v_uploadATF.get(threadName,None) and (self._v_uploadATF[threadName] is not None) and (not self._v_uploadATF[threadName].end) :
 2645: 
 2646:                 wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
 2647:                 if wait_template:
 2648:                         return wait_template[0][1]()
 2649:                 
 2650:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
 2651:                 return pt(txt='/uploadATFfinally',threadName=threadName)
 2652:             else:
 2653:               self.REQUEST.SESSION['idTmp']=None
 2654:               if RESPONSE is not None:
 2655:                   RESPONSE.redirect(self.absolute_url())
 2656: 
 2657:     def importFiles(self,comment="",author="" ,folderName="/Users/dwinter/atf", files=None,ext=None):
 2658:         """import files"""
 2659:         logging.debug("importFiles folderName=%s files=%s ext=%s"%(folderName,files,ext))
 2660:         root=self.cdli_main
 2661:         count=0
 2662:         if not files:
 2663:             files=os.listdir(folderName)
 2664:             
 2665:         for f in files:
 2666:             folder=f[0:3]
 2667:             f2=f[0:5]
 2668:             obj=self.ZopeFind(root,obj_ids=[folder])
 2669:             logging.debug("importFiles: folder=%s f2=%s obj=%s"%(folder,f2,obj)) 
 2670:             if ext:
 2671:                 ext.result="<p>adding: %s </p>"%f+ext.result
 2672: 
 2673:             if not obj:
 2674:                 manage_addCDLIFileFolder(root,folder,folder)
 2675:                 fobj=getattr(root,folder)
 2676:                 #transaction.get().commit()                           
 2677: 
 2678:             else:
 2679:                 fobj=obj[0][1]
 2680:             
 2681:             obj2=fobj.ZopeFind(fobj,obj_ids=[f2])
 2682:             logging.debug("importFiles: fobj=%s obj2=%s"%(fobj,obj2)) 
 2683:         
 2684:             if not obj2:
 2685:                 manage_addCDLIFileFolder(fobj,f2,f2)
 2686:                 fobj2=getattr(fobj,f2)
 2687:         
 2688:             else:
 2689:                 fobj2=obj2[0][1]
 2690:               
 2691:             file2=os.path.join(folderName,f)  
 2692:             id=f
 2693:             logging.debug("importFiles: addCDLIFile fobj2=%s, f=%s file2=%s"%(fobj2,repr(f),repr(file2)))
 2694:             fobj2.addFile(vC='',file=file(file2),author=author,newName=f)
 2695:             count+=1
 2696: 
 2697:             if count%100==0:
 2698:                 logging.debug("importfiles: committing")
 2699:                 transaction.get().commit()
 2700: 
 2701:         transaction.get().commit()
 2702:         return "ok"
 2703:          
 2704: 
 2705: manage_addCDLIRootForm=DTMLFile('dtml/rootAdd', globals())
 2706: 
 2707:     
 2708: def manage_addCDLIRoot(self, id, title='',
 2709:                      createPublic=0,
 2710:                      createUserF=0,
 2711:                      REQUEST=None):
 2712:     """Add a new Folder object with id *id*.
 2713: 
 2714:     If the 'createPublic' and 'createUserF' parameters are set to any true
 2715:     value, an 'index_html' and a 'UserFolder' objects are created respectively
 2716:     in the new folder.
 2717:     """
 2718:     ob=CDLIRoot()
 2719:     ob.id=str(id)
 2720:     ob.title=title
 2721:     try:
 2722:         self._setObject(id, ob)
 2723:     except:
 2724:         pass
 2725:     ob=self._getOb(id)
 2726: 
 2727:     checkPermission=getSecurityManager().checkPermission
 2728: 
 2729:     if createUserF:
 2730:         if not checkPermission('Add User Folders', ob):
 2731:             raise Unauthorized, (
 2732:                   'You are not authorized to add User Folders.'
 2733:                   )
 2734:         ob.manage_addUserFolder()
 2735: 
 2736:   
 2737:     if REQUEST is not None:
 2738:         return self.manage_main(self, REQUEST, update_menu=1)    
 2739:  

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>