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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/cedaMoles/libs/migration/processor/deployment_data.py@8538
Revision 8538, 10.6 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 13 Feb 2012
30
31@author: Maurizio Nagni
32'''
33from cedaMoles.libs.migration.processor.commons import find_subtype_in_dpt, \
34    create_ceda_processing, create_ceda_instrument, create_ceda_project, \
35    find_summary, find_documentation_in_migration_document, create_ci_citation, \
36    create_mo_online_resource, find_links_in_deployment, has_mo_been_processed, \
37    get_atom_document_hash_by_mo, calculate_hash, has_mo_same_hash
38from ea_model.moles3_4.utilities.ceda_publicationstatevalue \
39    import CEDA_PublicationStateValue
40from cedaMoles.MolesManager.codelist import getCLValue
41from cedaMoles.MolesManager.ceda_guid import CedaGUID
42import logging
43from logging import StreamHandler
44from ea_model.ceda_metadatamodel.ceda_project.ceda_project import CEDA_Project
45
46class DeploymentDataProcessor(object):
47    log = logging.getLogger('DeploymentDataProcessor')
48    log.addHandler(StreamHandler())
49    log.setLevel(logging.INFO) 
50   
51    def __init__(self, deploymentMigration, epbRepo):
52        '''
53            @param epbRepo: an instance of EPBRepo
54        '''       
55        self._deploymentMigration = deploymentMigration
56        self.epbRepo = epbRepo       
57        self._links = find_links_in_deployment(self._deploymentMigration)
58       
59    def _commitDeploymentMigration(self, associateWithCedaObservation, \
60                                   dataProductionTool, dataProductionToolField):
61        self.epbRepo.moles3EPB.persistInstance(associateWithCedaObservation)
62        self.epbRepo.migrationEPB.\
63            updateMigrationObject(dataProductionTool, \
64                            {dataProductionToolField: associateWithCedaObservation.id})
65                                   
66    def _createProject(self, activity):       
67        i_abstract = find_summary(activity)
68        doc_link = find_documentation_in_migration_document(activity)
69        i_documentation = None
70        i_resource = None
71        if doc_link and len(doc_link) == 1:
72            i_documentation = create_ci_citation("Documentation resource")
73            i_resource = create_mo_online_resource(doc_link[0]['href'])
74        project = create_ceda_project(abstract=i_abstract, \
75                    publication_state=getCLValue(CEDA_PublicationStateValue.cl_working), \
76                    documentation=i_documentation, project_resource=i_resource)
77        self._commitDeploymentMigration(project, activity, 'ceda_project_id')
78        return project
79               
80    def createProject(self):
81        project = None
82        if self._links.has_key('ACTIVITY'):
83            for link in self._links['ACTIVITY']:
84                activity = self.epbRepo.migrationEPB.\
85                    get_deployment_data_migration_by_name(self._deploymentMigration, \
86                                                     link + '.atom')
87                deploymentDataHasSameHash = has_mo_same_hash(activity)   
88                deploymentDataHasBeenProcessed = has_mo_been_processed(activity)               
89                try :
90                    if activity.ceda_project_id is not None:
91                        #obsColl = self.epbRepo.moles3EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id)
92                        project = self.epbRepo.moles3EPB.search(CEDA_Project, 
93                                                                activity.ceda_project_id)
94                    else:
95                        project = self._createProject(activity)
96                except Exception as ex:
97                    #self._report.append(ex)
98                    #return self._report
99                    pass 
100                       
101                #Is a first time process?
102                if not deploymentDataHasBeenProcessed:
103                    docHash = get_atom_document_hash_by_mo(activity)
104                    self.epbRepo.migrationEPB.updateMigrationObject(activity, \
105                        {'ceda_project_id': project.id, \
106                         'doc_hash': docHash})
107
108                #Has to updated the hash?
109                if not deploymentDataHasSameHash and deploymentDataHasBeenProcessed:
110                    docHash = get_atom_document_hash_by_mo(activity)
111                    self.epbRepo.migrationEPB.updateMigrationObject(activity, \
112                        {'doc_hash': docHash})
113               
114                #Has a proper CEDAGUID?
115                if self.epbRepo.moles3EPB.retrieveGUIDFromInstance(project) is None:
116                    #Adds the CedaGUID
117                    ceda_guid = CedaGUID()
118                    ceda_guid.id = calculate_hash(activity.deployment_data_id)
119                    setattr(ceda_guid, 'ceda_project', project.id)
120                    self.epbRepo.moles3EPB.persistInstance(ceda_guid)
121                    DeploymentDataProcessor.log.info("GUID for this Project: %s" % (ceda_guid.id))                   
122        return project
123       
124    def createProcess(self):
125        associateWithCedaObservation = None
126        hasCedaProcessing = False
127        hasCedaComposite = False
128        hasCedaAcquisition = False
129        for dpt in self._links['DPT']:
130            dataProductionTool = self.epbRepo.migrationEPB.\
131                get_deployment_data_migration_by_name(self._deploymentMigration, \
132                                                      dpt + '.atom')
133           
134            #has the document changed?           
135            #if has_mo_same_hash(dataProductionTool):
136            #    dataProductionTool.doc_hash = get_atom_document_hash_by_mo(self._deploymentMigration)
137            #    self._migrationSessions.migrationSession.commit()
138            #    continue
139           
140            subType = find_subtype_in_dpt(dataProductionTool)
141           
142            if subType == 'model':
143                #MigrationEPB.loadAttributes(dataProductionTool, 'ceda_processing_id')
144                if dataProductionTool.ceda_processing_id is None:
145                    associateWithCedaObservation = create_ceda_processing()
146                    self._commitDeploymentMigration(associateWithCedaObservation, 
147                                                    dataProductionTool, 
148                                                    'ceda_processing_id')
149                    if not (hasCedaComposite or hasCedaProcessing):
150                        hasCedaProcessing = True
151                        hasCedaComposite = False
152                        hasCedaAcquisition = False
153                else:
154                    #should update
155                    pass 
156            else: 
157                #MigrationEPB.loadAttributes(dataProductionTool, 'ceda_instrument_id')
158                if dataProductionTool.ceda_instrument_id is None:
159                    associateWithCedaObservation = create_ceda_instrument()
160                    self._commitDeploymentMigration(associateWithCedaObservation, dataProductionTool, 'ceda_instrument_id')
161                else:
162                    #should update
163                    pass
164               
165               
166        #if not a DPT.subType == 'model' then....
167        for obs in self._links['OBS']:
168            observationStation = self.epbRepo.migrationEPB.get_deployment_data_migration_by_name(self._deploymentMigration, obs + '.atom')
169 
170            #has the document changed?           
171            #if has_mo_same_hash(observationStation):
172            #    observationStation.doc_hash = get_atom_document_hash_by_mo(self._deploymentMigration)
173            #    self._migrationSessions.migrationSession.commit()
174            #    continue                       
175           
176            subType = find_subtype_in_dpt(observationStation)
177            if subType == 'satellite':
178                #MigrationEPB.loadAttributes(dataProductionTool, 'ceda_compositeprocess_id')               
179                if dataProductionTool.ceda_compositeprocess_id is None:
180                    associateWithCedaObservation = create_ceda_processing()
181                    self._commitDeploymentMigration(associateWithCedaObservation, dataProductionTool, 'ceda_compositeprocess_id')   
182                    if not hasCedaComposite:
183                        hasCedaProcessing = True
184                        hasCedaComposite = False
185                        hasCedaAcquisition = False
186                else:
187                    #should update
188                    pass
189            else:
190                #MigrationEPB.loadAttributes(dataProductionTool, 'ceda_acquisition_id')               
191                if dataProductionTool.ceda_acquisition_id is None:
192                    pass
193                    '''
194                    associateWithCedaObservation = create_ceda_acquisition()
195                    self._commitDeploymentMigration(associateWithCedaObservation, dataProductionTool, 'ceda_acquisition_id')
196                    '''               
197                else:
198                    #should update
199                    pass
200                    if not (hasCedaComposite or hasCedaProcessing or hasCedaAcquisition):
201                        hasCedaProcessing = True
202                        hasCedaComposite = False
203                        hasCedaAcquisition = True                           
204        return associateWithCedaObservation                           
205       
Note: See TracBrowser for help on using the repository browser.