source: mauRepo/MolesManager/trunk/cedaMoles/libs/migration/processor/loadResources.py @ 8538

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/cedaMoles/libs/migration/processor/loadResources.py@8538
Revision 8538, 6.4 KB checked in by mnagni, 7 years ago (diff)

Incomplete - # 22528: Migration of FAtCat Open Search link for HPFeld
 http://team.ceda.ac.uk/trac/ceda/ticket/22528

Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 15 Nov 2011
30
31@author: Maurizio Nagni
32'''
33from cedaMoles.libs.migration.processor.commons import DOC_STATUS, get_collection_refs, \
34    find_moles_creation_date, find_id, stringToTimestamp, build_exist_type_path, \
35    build_exist_doc_path, DT_DATA_ENTITIES, DT_DEPLOYMENTS, DT_DEPLOYMENT_DATA, \
36    get_owner_refs, get_atom_document_as_elementtree
37from cedaMoles.libs.migration.exception.exceptions import NoCreationDate, NoDocumentID
38from cedaMoles.libs.migration.db.classes import DeploymentDataMigration, \
39    DeploymentsMigration, DataEntityMigration
40
41class LoadResources(object):
42
43    def __init__(self, epbRepo):
44        '''
45            @param epbRepo: an instance of EPBRepo
46        '''
47        self.epbRepo = epbRepo
48
49    def process(self):
50        """
51            Loads all the folders below the atoms ('published', 'working', 'Published') folders
52        """ 
53        ex = []       
54        for status in DOC_STATUS:       
55            ex.extend(self._loadCollections(status, DT_DATA_ENTITIES, DataEntityMigration))
56            ex.extend(self._loadCollections(status, DT_DEPLOYMENTS, DeploymentsMigration))
57            ex.extend(self._loadCollections(status, DT_DEPLOYMENT_DATA, DeploymentDataMigration))                                               
58        return ex
59
60    def _createMigrationObject(self, migrationClass, doc_id, docName, docOwner, docStatus, docCreation):
61        migrationObject = None
62        if migrationClass == DeploymentsMigration:
63            migrationObject = DeploymentsMigration()
64            migrationObject.depl_id = doc_id
65        elif  migrationClass == DataEntityMigration:
66            migrationObject = DataEntityMigration()
67            migrationObject.data_ent_id = doc_id
68        elif migrationClass == DeploymentDataMigration:
69            migrationObject = DeploymentDataMigration()
70            migrationObject.deployment_data_id = doc_id
71         
72        if migrationObject is None:
73            raise Exception("migrationObject is None")
74        migrationObject.doc_name = docName
75        migrationObject.doc_owner = docOwner
76        migrationObject.doc_status = docStatus                     
77        migrationObject.doc_creation = stringToTimestamp(docCreation)
78        try:
79            self.epbRepo.migrationEPB.persistInstance(migrationObject)
80        except Exception as e:
81            print e
82
83    def _loadCollections(self, docStatus, docType, migrationClass):
84        ex = []
85        refs = get_collection_refs(build_exist_type_path(docStatus, docType))           
86        for ref in refs:
87            docOwner = ref.get('name') 
88            if docOwner.startswith('.'):
89                continue                   
90            ex.extend(self._loadMigrationDocs(docStatus, docOwner, docType, migrationClass))
91
92        return ex
93
94    def _loadMigrationDocs(self, docStatus, docOwner, docType, migrationClass):
95        ex = []
96        refs = get_owner_refs(docStatus, docType, docOwner)                   
97        for ref in refs:
98            docName = ref.get('name')
99            try:
100                self._processMigrationDoc(docName, docStatus, docOwner, docType, migrationClass)
101            except Exception as e:
102                if hasattr(e, 'value'):
103                    e.value = build_exist_doc_path(docStatus, docType, docOwner, docName)
104                ex.append(e)
105        return ex
106
107    def _processMigrationDoc(self, docName, docStatus, docOwner, docType, migrationClass):
108        """
109            Ingests documents like DataEntity, Deployment, Deployment_Data. The document is register in a DB table of the appropriate
110            type (DataEntityMigration, DeploymentsMigration, DeploymentDataMigration).
111            @param docName: the document name
112            @param docStatus: a common.docStatus item
113            @param docOwner: a common.docOwner item   
114        """
115         
116        #The docHash has to be set/check when the document is processed! 
117        xmlDocument = get_atom_document_as_elementtree(docStatus, docType, docOwner, docName)
118        doc_id = find_id(xmlDocument)
119         
120        #The document has been already classified
121        if doc_id is None or self.epbRepo.migrationEPB.search(migrationClass, doc_id) is not None:
122            return
123       
124                         
125        #The document shall be classified                               
126        docCreation = self._extractCreationDate(xmlDocument)       
127        self._createMigrationObject(migrationClass, doc_id, docName, docOwner, docStatus, docCreation)
128
129
130    def _extractID(self, xmlDocument):
131        doc_id = find_id(xmlDocument) 
132        if doc_id is None:
133            raise NoDocumentID(doc_id)
134        return doc_id
135
136    def _extractCreationDate(self, xmlDocument):
137        creationDate = find_moles_creation_date(xmlDocument)           
138        if creationDate is None:
139            raise NoCreationDate(creationDate)
140        return creationDate
141           
142
143       
Note: See TracBrowser for help on using the repository browser.