001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
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
021package org.granite.builder.properties;
022
023import org.granite.builder.util.XStreamUtil;
024import org.granite.generator.as3.DefaultAs3TypeFactory;
025import org.granite.generator.as3.DefaultEntityFactory;
026import org.granite.generator.as3.DefaultRemoteDestinationFactory;
027import org.granite.generator.as3.JavaAs3GroovyTransformer;
028import org.granite.generator.as3.reflect.JavaType.Kind;
029import org.granite.generator.template.StandardTemplateUris;
030
031import com.thoughtworks.xstream.annotations.XStreamAlias;
032import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
033
034/**
035 * @author Franck WOLFF
036 */
037@XStreamAlias(value="graniteProperties")
038public 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        @Override
076        public void validate(ValidationResults results) {
077                if (gas3 != null)
078                        gas3.validate(results);
079                
080                if (!CURRENT_VERSION.equals(version)) {
081                        if (VERSION_1_0.equals(version)) {
082                                Gas3Template template = gas3.getTemplate(Kind.INTERFACE);
083                                gas3.getTemplates().remove(template);
084                                gas3.getTemplates().add(new Gas3Template(Kind.INTERFACE, StandardTemplateUris.INTERFACE));
085                                results.getWarnings().add("Base template for interfaces is deprecated (ignored)");
086                        }
087                        else
088                                results.getWarnings().add("Unknown graniteProperties version: " + version);
089                }
090                
091                if (gas3.getTemplate(Kind.REMOTE_DESTINATION) == null) {
092                        StringBuilder uris = new StringBuilder(StandardTemplateUris.REMOTE);
093                        uris.append(';');
094                        if (gas3.getTemplate(Kind.ENTITY).getUris().endsWith(StandardTemplateUris.TIDE_ENTITY_BASE))
095                                uris.append(StandardTemplateUris.TIDE_REMOTE_BASE);
096                        else
097                                uris.append(StandardTemplateUris.REMOTE_BASE);
098                        gas3.getTemplates().add(new Gas3Template(Kind.REMOTE_DESTINATION, uris.toString()));
099                }
100                
101                if (gas3.getEntityFactory() == null)
102                        gas3.setEntityFactory(DefaultEntityFactory.class.getName());
103                
104                if (gas3.getRemoteDestinationFactory() == null)
105                        gas3.setRemoteDestinationFactory(DefaultRemoteDestinationFactory.class.getName());
106        }
107        
108        @Override
109        public String toString() {
110                return XStreamUtil.toString(this);
111        }
112
113        public static GraniteProperties getDefaultProperties() {
114                Gas3 gas3 = new Gas3("uid", DefaultAs3TypeFactory.class.getName(), DefaultEntityFactory.class.getName(), DefaultRemoteDestinationFactory.class.getName());
115
116                gas3.getTemplates().add(new Gas3Template(
117                        Kind.BEAN,
118                        StandardTemplateUris.BEAN + ";" + StandardTemplateUris.BEAN_BASE
119                ));
120
121                gas3.getTemplates().add(new Gas3Template(
122                        Kind.ENTITY,
123                        StandardTemplateUris.ENTITY + ";" + StandardTemplateUris.ENTITY_BASE
124                ));
125
126                gas3.getTemplates().add(new Gas3Template(
127                        Kind.INTERFACE,
128                        StandardTemplateUris.INTERFACE
129                ));
130
131                gas3.getTemplates().add(new Gas3Template(
132                        Kind.ENUM,
133                        StandardTemplateUris.ENUM
134                ));
135                
136                gas3.getTemplates().add(new Gas3Template(
137                        Kind.REMOTE_DESTINATION,
138                        StandardTemplateUris.REMOTE + ";" + StandardTemplateUris.REMOTE_BASE
139                ));
140                
141                gas3.getTransformers().add(new Gas3Transformer(JavaAs3GroovyTransformer.class.getName()));
142                
143                GraniteProperties properties = new GraniteProperties();
144                properties.setGas3(gas3);
145                return properties;
146        }
147}