001: /*
002: * Copyright 2007-2008 Hippo
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS"
012: * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cms.wizard.resources;
017:
018: import java.util.Random;
019:
020: import nl.hippo.cms.wizard.Constants;
021: import nl.hippo.cms.wizard.WizardResult;
022: import nl.hippo.cms.wizard.components.AbstractComponentContainer;
023: import nl.hippo.cms.wizard.components.Component;
024: import nl.hippo.cms.wizard.components.Configuration;
025: import nl.hippo.cms.wizard.widgets.Widget;
026: import nl.hippo.uidgenerator.UIDGenerator;
027: import nl.hippo.uidgenerator.UIDGeneratorException;
028:
029: import org.springframework.web.context.WebApplicationContext;
030: import org.xml.sax.SAXException;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: public abstract class AbstractResource extends
035: AbstractComponentContainer implements Resource {
036:
037: private static final String RESOURCE_EXTENSION = "xml";
038:
039: // value of @name of engine:document element, is used for skipping the widget in the path creation
040: private String widgetWithValueForResourceName;
041: private String type;
042: private boolean lowercase;
043:
044: private boolean autoCreateResourceName = true;
045:
046: private final Log logger = LogFactory
047: .getLog(AbstractResource.class);
048:
049: public void configure(Configuration config) throws Exception {
050: super .configure(config);
051:
052: String name = config.get(Constants.NAME_ATTRIBUTE_NAME);
053: if (name != null
054: && !name.equals("")
055: && !name
056: .equals(Constants.AUTO_CREATE_RESOURCE_NAME_FLAG)) {
057: widgetWithValueForResourceName = getId()
058: + Constants.ID_DELIMITER + name;
059: autoCreateResourceName = false;
060: }
061: lowercase = (config.get(Constants.LOWERCASE_ATTRIBUTE_NAME) != null) ? config
062: .get(Constants.LOWERCASE_ATTRIBUTE_NAME).equals("true")
063: : false;
064: if (lowercase)
065: lowerCasePath();
066:
067: String type = config.get(Constants.TYPE_ATTRIBUTE_NAME);
068: if (type == null || type.equals(""))
069: throw new SAXException("resource:*/@"
070: + Constants.TYPE_ATTRIBUTE_NAME
071: + " is required and cannot be empty");
072: this .type = type;
073: }
074:
075: /**
076: * If @name is set to an widget-id, check if it's found
077: */
078: //@Override
079: public void initialize() throws SAXException {
080: if (!autoCreateResourceName) {
081: Component component = findComponentById(widgetWithValueForResourceName);
082: if (component == null) {
083: throw new SAXException(
084: "Resource["
085: + getType()
086: + "]@id="
087: + getId()
088: + " has attribute name set to '"
089: + widgetWithValueForResourceName
090: + "', but no widget with a corresponding @id is found");
091: }
092: }
093: }
094:
095: //@Override
096: public void addComponent(Widget widget) {
097: super .addComponent(widget);
098: }
099:
100: protected String getName() {
101: if (autoCreateResourceName) {
102: try {
103: return UIDGenerator.generateUID();
104: } catch (UIDGeneratorException e) {
105: e.printStackTrace();
106: }
107: } else {
108: Widget nameWidget = (Widget) components
109: .get(widgetWithValueForResourceName);
110: if (nameWidget != null) {
111: return nameWidget.getURIValue();
112: }
113: }
114: //use lame fallback
115: return fallBackIdGenerator();
116: }
117:
118: private String fallBackIdGenerator() {
119: Random rnd = new Random();
120: long uniqueId = ((System.currentTimeMillis() >>> 16) << 16)
121: + rnd.nextInt();
122: return Long.toString(uniqueId);
123: }
124:
125: //@Override
126: public void fillResult(WizardResult result,
127: WebApplicationContext model) throws Exception {
128: super .fillResult(result, model);
129:
130: result.setExtension(RESOURCE_EXTENSION);
131: if (lowercase) {
132: if (logger.isDebugEnabled()) {
133: logger.debug("Set name with lowercase");
134: }
135: result.setName(getName().toLowerCase());
136: } else {
137: result.setName(getName());
138: }
139: result.setType(type);
140: }
141:
142: }
|