[opencms-dev] changing types and template-properties on multiplefiles

Jonathan Woods jonathan.woods at scintillance.com
Thu Nov 9 16:10:55 CET 2006


Marc -

Here's some code I used to change resource type and properties when
importing data.  Hope you can make some sense and some use of it; also
attached.

Jon
====================

	/**
	 * Assumes context is such that we're in the / site.  Steals locks,
so should be used in single-user mode.
	 * 
	 * @throws CmsException
	 */
	public void completeCalendarEntryImport() throws CmsException {
		// Loop through all resources in the calendar entry folder.

		final CmsResourceFilter filter =
CmsResourceFilter.ALL.addExcludeState(CmsResource.STATE_DELETED);
		final List<CmsResource> calendarEntries =
this.cmsObject.getFilesInFolder("/sites/default/calendarentries", filter);

		for (final CmsResource cmsResource : calendarEntries) {
			final String absoluteResourcePath =
cmsResource.getRootPath();
			System.out.println("Handling " +
absoluteResourcePath);

			this.cmsObject.lockResource(absoluteResourcePath);
			
			// Change the resource's type.

			this.cmsObject.chtype(absoluteResourcePath,
ConfigurationParameters.CALENDAR_ENTRY_TYPE_ID);

			// Get hold of the resource as an XML document.

			CmsFile resourceFile = null;

			try {
				resourceFile = CmsFile.upgrade(cmsResource,
this.cmsObject);

				I_CmsXmlDocument xmlDocument =
CmsXmlContentFactory.unmarshal(this.cmsObject, resourceFile);

				// Get Title, DateReleased, DateExpired,
EnableNotification, NotificationInterval elements and map to
				// resource properties or attributes as
appropriate. First, attributes.

				String s =
xmlDocument.getStringValue(this.cmsObject, "DateReleased",
ConfigurationParameters.PRIMARY_CONTENT_LOCALE_LANGUAGE_ONLY);
				if (s != null) {
	
cmsResource.setDateReleased(Long.valueOf(s).longValue());
				}

				s =
xmlDocument.getStringValue(this.cmsObject, "DateExpired",
ConfigurationParameters.PRIMARY_CONTENT_LOCALE_LANGUAGE_ONLY);
				if (s != null) {
	
cmsResource.setDateExpired(Long.valueOf(s).longValue());
				}

				// Now, properties.

				final List<CmsProperty> propertiesToChange =
new ArrayList<CmsProperty>();

				s =
xmlDocument.getStringValue(this.cmsObject, "Title",
ConfigurationParameters.PRIMARY_CONTENT_LOCALE_LANGUAGE_ONLY);
				if (s != null) {
					propertiesToChange.add(new
CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, s, null, true));
				}

				s =
xmlDocument.getStringValue(this.cmsObject, "EnableNotification",
ConfigurationParameters.PRIMARY_CONTENT_LOCALE_LANGUAGE_ONLY);
				if (s != null) {
					propertiesToChange.add(new
CmsProperty(CmsPropertyDefinition.PROPERTY_ENABLE_NOTIFICATION, s, null,
true));
				}

				s =
xmlDocument.getStringValue(this.cmsObject, "NotificationInterval",
ConfigurationParameters.PRIMARY_CONTENT_LOCALE_LANGUAGE_ONLY);
				if (s != null) {
					propertiesToChange.add(new
CmsProperty(CmsPropertyDefinition.PROPERTY_NOTIFICATION_INTERVAL, s, null,
true));
				}

				// Now set other properties required by our
site machinery: template-elements and content-conversion.
				
				propertiesToChange.add(new
CmsProperty("content-conversion", "cleanup;xhtml", null, true));
				propertiesToChange.add(new
CmsProperty("template-elements",
"/system/modules/com.scintillance.opencms.module/template-elements/calendare
ntry.jsp", null, true));

				// Write out the properties.

	
this.cmsObject.writePropertyObjects(absoluteResourcePath,
propertiesToChange);

				// Finally, unlock the resource.
				
	
this.cmsObject.unlockResource(absoluteResourcePath);
			}
			catch (CmsException e) {
				// Do the best we can - just log the problem
and carry on with the next resource.

				LOG.debug("For resource " +
absoluteResourcePath, e);
				continue;
			}
		}
	} 

-----Original Message-----
From: opencms-dev-bounces at opencms.org
[mailto:opencms-dev-bounces at opencms.org] On Behalf Of Marc Johnen
Sent: 09 November 2006 14:15
To: opencms-dev at opencms.org
Subject: [opencms-dev] changing types and template-properties on
multiplefiles

Hello everyone,

because all the types and properties of a folder vanished, i have to set
them again. Is there a way to change the type on multiple files?
i'm talking about MANY files.

Also changing the template-property with the "Change Property
Values"-function only works on properties which are already set and not on
those which are empty (or null?). For the "Old property value" i use .* .
Maybe i'm just not good enough at regular expressions.

Can anyone help me?

Thanks and greetins
Marc Johnen, Berlin

_______________________________________________
This mail is sent to you from the opencms-dev mailing list To change your
list options, or to unsubscribe from the list, please visit
http://lists.opencms.org/mailman/listinfo/opencms-dev

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: completeCalendarEntryImport.txt
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20061109/19e71e4b/attachment.txt>


More information about the opencms-dev mailing list