001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.builder.properties;
022
023 import org.granite.builder.util.XStreamUtil;
024 import org.granite.generator.as3.DefaultAs3TypeFactory;
025 import org.granite.generator.as3.DefaultEntityFactory;
026 import org.granite.generator.as3.DefaultRemoteDestinationFactory;
027 import org.granite.generator.as3.JavaAs3GroovyTransformer;
028 import org.granite.generator.as3.reflect.JavaType.Kind;
029 import org.granite.generator.template.StandardTemplateUris;
030
031 import com.thoughtworks.xstream.annotations.XStreamAlias;
032 import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
033
034 /**
035 * @author Franck WOLFF
036 */
037 @XStreamAlias(value="graniteProperties")
038 public class GraniteProperties implements Validable {
039
040 private transient long timestamp = -1L;
041
042 public static final String VERSION_1_0 = "1.0";
043 public static final String VERSION_2_0 = "2.0";
044 public static final String CURRENT_VERSION = VERSION_2_0;
045
046 @XStreamAsAttribute
047 private String version = VERSION_2_0;
048
049 private Gas3 gas3;
050
051 public long getTimestamp() {
052 return timestamp;
053 }
054
055 public void setTimestamp(long timestamp) {
056 this.timestamp = timestamp;
057 }
058
059 public String getVersion() {
060 return version;
061 }
062
063 public void setVersion(String version) {
064 this.version = version;
065 }
066
067 public Gas3 getGas3() {
068 return gas3;
069 }
070
071 public void setGas3(Gas3 gas3) {
072 this.gas3 = gas3;
073 }
074
075 public void validate(ValidationResults results) {
076 if (gas3 != null)
077 gas3.validate(results);
078
079 if (!CURRENT_VERSION.equals(version)) {
080 if (VERSION_1_0.equals(version)) {
081 Gas3Template template = gas3.getTemplate(Kind.INTERFACE);
082 gas3.getTemplates().remove(template);
083 gas3.getTemplates().add(new Gas3Template(Kind.INTERFACE, StandardTemplateUris.INTERFACE));
084 results.getWarnings().add("Base template for interfaces is deprecated (ignored)");
085 }
086 else
087 results.getWarnings().add("Unknown graniteProperties version: " + version);
088 }
089
090 if (gas3.getTemplate(Kind.REMOTE_DESTINATION) == null) {
091 StringBuilder uris = new StringBuilder(StandardTemplateUris.REMOTE);
092 uris.append(';');
093 if (gas3.getTemplate(Kind.ENTITY).getUris().endsWith(StandardTemplateUris.TIDE_ENTITY_BASE))
094 uris.append(StandardTemplateUris.TIDE_REMOTE_BASE);
095 else
096 uris.append(StandardTemplateUris.REMOTE_BASE);
097 gas3.getTemplates().add(new Gas3Template(Kind.REMOTE_DESTINATION, uris.toString()));
098 }
099
100 if (gas3.getEntityFactory() == null)
101 gas3.setEntityFactory(DefaultEntityFactory.class.getName());
102
103 if (gas3.getRemoteDestinationFactory() == null)
104 gas3.setRemoteDestinationFactory(DefaultRemoteDestinationFactory.class.getName());
105 }
106
107 @Override
108 public String toString() {
109 return XStreamUtil.toString(this);
110 }
111
112 public static GraniteProperties getDefaultProperties() {
113 Gas3 gas3 = new Gas3("uid", DefaultAs3TypeFactory.class.getName(), DefaultEntityFactory.class.getName(), DefaultRemoteDestinationFactory.class.getName());
114
115 gas3.getTemplates().add(new Gas3Template(
116 Kind.BEAN,
117 StandardTemplateUris.BEAN + ";" + StandardTemplateUris.BEAN_BASE
118 ));
119
120 gas3.getTemplates().add(new Gas3Template(
121 Kind.ENTITY,
122 StandardTemplateUris.ENTITY + ";" + StandardTemplateUris.ENTITY_BASE
123 ));
124
125 gas3.getTemplates().add(new Gas3Template(
126 Kind.INTERFACE,
127 StandardTemplateUris.INTERFACE
128 ));
129
130 gas3.getTemplates().add(new Gas3Template(
131 Kind.ENUM,
132 StandardTemplateUris.ENUM
133 ));
134
135 gas3.getTemplates().add(new Gas3Template(
136 Kind.REMOTE_DESTINATION,
137 StandardTemplateUris.REMOTE + ";" + StandardTemplateUris.REMOTE_BASE
138 ));
139
140 gas3.getTransformers().add(new Gas3Transformer(JavaAs3GroovyTransformer.class.getName()));
141
142 GraniteProperties properties = new GraniteProperties();
143 properties.setGas3(gas3);
144 return properties;
145 }
146 }