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

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

Incomplete - # 22534: Add versiojn number to the gui page
 http://team.ceda.ac.uk/trac/ceda/ticket/22534

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