| 1 | from zope.interface import implements |
|---|
| 2 | from collective.imstransport.utilities.interfaces import IIMSManifestWriter |
|---|
| 3 | from collective.imstransport.utilities.packagingio import ZipfileWriter |
|---|
| 4 | from collective.imstransport.utilities.imscc.ccwriter import CCWriter |
|---|
| 5 | from collective.imstransport.utilities.imscc.ccresourcewriter import CCResourceWriter |
|---|
| 6 | from collective.imstransport.utilities.imsinterchange import IMSWriter |
|---|
| 7 | from collective.imstransport import IMSTransportMessageFactory as _ |
|---|
| 8 | |
|---|
| 9 | class IMSCCWriter(IMSWriter): |
|---|
| 10 | """ Write an IMS content package manifest file. """ |
|---|
| 11 | |
|---|
| 12 | name = _(u'IMS Common Cartridge') |
|---|
| 13 | |
|---|
| 14 | def createPackage(self, file, context): |
|---|
| 15 | """ Creates an IMS Package """ |
|---|
| 16 | |
|---|
| 17 | destination = ZipfileWriter(context, file) |
|---|
| 18 | |
|---|
| 19 | ccw = CCWriter() |
|---|
| 20 | |
|---|
| 21 | # Create the Manifest |
|---|
| 22 | manId = self._createPathId(context.virtual_url_path(), 'MAN') |
|---|
| 23 | manVer = context.ModificationDate() |
|---|
| 24 | doc = ccw.createManifest(manId, manVer) |
|---|
| 25 | |
|---|
| 26 | # Add top level metadata |
|---|
| 27 | lang = context.Language() |
|---|
| 28 | if not lang: |
|---|
| 29 | lang = context.portal_properties.site_properties.getProperty('default_language') |
|---|
| 30 | ccw.writeTopLevelMetadata(doc, |
|---|
| 31 | context.getId(), |
|---|
| 32 | context.Title(), |
|---|
| 33 | context.portal_url(), |
|---|
| 34 | lang, |
|---|
| 35 | context.Description(), |
|---|
| 36 | context.Subject()) |
|---|
| 37 | |
|---|
| 38 | # Write Organizations and Resources |
|---|
| 39 | orgId = self._createPathId(context.virtual_url_path(), 'ORG') |
|---|
| 40 | itemId = self._createPathId(context.virtual_url_path(), 'ITM') |
|---|
| 41 | orgs = ccw.createOrganizations(doc, orgId, itemId) |
|---|
| 42 | res = ccw.createResources(doc) |
|---|
| 43 | |
|---|
| 44 | if orgs and res: |
|---|
| 45 | objs = self._getAllObjects(context) |
|---|
| 46 | for obj in objs: |
|---|
| 47 | # Need to consider excluding folders |
|---|
| 48 | # Get resource info |
|---|
| 49 | path = self._getObjectPath(obj, context) |
|---|
| 50 | vpath = obj.virtual_url_path() |
|---|
| 51 | refid = self._createPathId(vpath, 'RES') |
|---|
| 52 | # Check if we need to add to the organizations section |
|---|
| 53 | if not obj.getExcludeFromNav(): |
|---|
| 54 | itemId = self._createPathId(vpath, 'ITM') |
|---|
| 55 | ccw.writeItem(orgs, itemId, refid, obj.title) |
|---|
| 56 | rn = ccw.writeResource(res, refid, obj.Type(), path) |
|---|
| 57 | |
|---|
| 58 | id = obj.getId() |
|---|
| 59 | title = obj.Title() |
|---|
| 60 | urlbase = obj.portal_url() |
|---|
| 61 | lang = obj.Language() |
|---|
| 62 | if not lang: |
|---|
| 63 | lang = obj.portal_properties.site_properties.getProperty('default_language') |
|---|
| 64 | desc = obj.Description() |
|---|
| 65 | kw = obj.Subject() |
|---|
| 66 | creators = obj.Creators() |
|---|
| 67 | contrib = obj.Contributors() |
|---|
| 68 | mod = obj.ModificationDate().replace(' ','T') |
|---|
| 69 | email = obj.portal_url.getPortalObject().getProperty('email_from_address') |
|---|
| 70 | format = obj.Format() |
|---|
| 71 | size = self.getObjSize(obj) |
|---|
| 72 | location = obj.renderBase() |
|---|
| 73 | value = 'yes' |
|---|
| 74 | rights_holder = obj.portal_properties.site_properties.getProperty('rights_holder') |
|---|
| 75 | rights_holder_email = obj.portal_properties.site_properties.getProperty('rights_holder_email') |
|---|
| 76 | copyright = self._getCopyrightString(obj.Rights(), rights_holder, rights_holder_email) |
|---|
| 77 | |
|---|
| 78 | md = ccw.createResourceMetadata(rn) |
|---|
| 79 | ccw.writeGeneralNode(md, id, title, urlbase, lang, desc, kw) |
|---|
| 80 | ccw.writeLifeCycleNode(md, creators, contrib, mod, lang) |
|---|
| 81 | ccw.writeMetaMetadataNode(md, id, urlbase, email, mod, lang, contrib) |
|---|
| 82 | ccw.writeTechnicalNode(md, format, size, location) |
|---|
| 83 | ccw.writeRightsNode(md, value, copyright, lang) |
|---|
| 84 | |
|---|
| 85 | if obj.Type() == 'Link': |
|---|
| 86 | link = ccw.getLinkXml(title, obj.getRemoteUrl()) |
|---|
| 87 | rpath = '%s%s' %(path,'.xml') |
|---|
| 88 | self._writeObjectData(link, rpath, destination) |
|---|
| 89 | ccw.writeResourceFile(rn, rpath) |
|---|
| 90 | else: |
|---|
| 91 | self._writeObjectData(obj, path, destination) |
|---|
| 92 | ccw.writeResourceFile(rn, path) |
|---|
| 93 | |
|---|
| 94 | self._writeObjectData(ccw.getManifest(doc), 'imsmanifest.xml', destination) |
|---|
| 95 | |
|---|
| 96 | if destination: |
|---|
| 97 | return destination.getOutput() |
|---|
| 98 | else: |
|---|
| 99 | return None, None |
|---|
| 100 | |
|---|
| 101 | |
|---|