]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/GraphBundleEx.java
f90896fa1d939ba459cd42c785c7cad1de1aabd6
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / GraphBundleEx.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.project.management;
13
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16
17 import org.eclipse.equinox.p2.metadata.IVersionedId;
18 import org.eclipse.equinox.p2.metadata.Version;
19 import org.eclipse.equinox.p2.metadata.VersionedId;
20 import org.simantics.databoard.binding.error.RuntimeBindingException;
21 import org.simantics.graph.representation.TransferableGraph1;
22
23 /**
24  * Graph Bundle extended. 
25  *
26  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27  */
28 public class GraphBundleEx extends GraphBundle implements IVersionedId {
29         
30         public static final SimpleDateFormat QUALIFIER_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
31         
32         public static GraphBundleEx extend(GraphBundle bundle) {
33                 return new GraphBundleEx(bundle);
34         }
35         
36         /**
37          * Replace "qualifier" string in version with current time in format of yyyyMMddHHmm. 
38          * 
39          * @param v version
40          * @return v or new version with current time
41          */
42         public static Version buildQualifier(Version v) {
43                 String qualifier = v.getSegment(3).toString();
44                 if (!qualifier.equals("qualifier")) return v;
45                 
46                 int major = (Integer) v.getSegment(0);
47                 int minor = (Integer) v.getSegment(1);
48                 int micro = (Integer) v.getSegment(2);          
49                 qualifier = QUALIFIER_FORMAT.format( new Date() );
50                 
51                 return Version.createOSGi(major, minor, micro, qualifier);
52         }
53         
54         public static VersionedId buildQualifier(IVersionedId vid) {
55                 Version v = vid.getVersion();
56                 String id = vid.getId();
57                 return new VersionedId(id, buildQualifier(v));
58         }
59         
60         VersionedId vid;
61         
62         GraphBundleEx(GraphBundle e) {
63                 this.graph = e.graph;
64                 this.resource = e.resource;
65                 this.hashcode = e.hashcode;
66                 this.id = e.id;
67                 this.major = e.major;
68                 this.minor = e.minor;
69                 this.service = e.service;
70                 this.qualifier = e.qualifier;
71                 this.resourceArray = e.resourceArray;
72                 this.name = e.name;
73                 this.vid = new VersionedId(id, Version.createOSGi(major, minor, service, qualifier));
74                 this.immutable = e.immutable;
75         }
76         
77         public GraphBundleEx(String name, TransferableGraph1 data, String versionedId) 
78         throws RuntimeBindingException 
79         {
80                 super(name, data, versionedId);
81                 Version v = Version.createOSGi(major, minor, service, qualifier);
82                 vid = new VersionedId(id, v);
83         }       
84         
85         public GraphBundleEx(String name, TransferableGraph1 data, String id, String version) 
86         throws RuntimeBindingException 
87         {
88                 super(name, data, id, version);
89                 Version v = Version.createOSGi(major, minor, service, qualifier);
90                 vid = new VersionedId(id, v);
91         }       
92
93         public GraphBundleEx(String name, TransferableGraph1 data, String id, Version version) 
94         throws RuntimeBindingException 
95         {
96                 super(name, data, id, version.getSegment(0)+"."+version.getSegment(1)+"."+version.getSegment(2)+"."+version.getSegment(3));
97                 Version v = Version.createOSGi(major, minor, service, qualifier);
98                 vid = new VersionedId(id, v);
99         }       
100         
101         public GraphBundleEx(String name, TransferableGraph1 data, IVersionedId vid, boolean isImmutable) 
102         throws RuntimeBindingException 
103         {
104                 super(name, data, vid.getId(), vid.getVersion().getSegment(0).toString()+"."+vid.getVersion().getSegment(1).toString()+"."+vid.getVersion().getSegment(2).toString()+"."+vid.getVersion().getSegment(3).toString());
105                 this.vid = new VersionedId(id, vid.getVersion());
106                 this.immutable = isImmutable;
107         }
108
109         public GraphBundleEx(String name, TransferableGraph1 data, IVersionedId vid) 
110         throws RuntimeBindingException 
111         {
112                 this(name, data, vid, true);
113         }
114         
115         /**
116          * Converts "qualifier" into current time in format of "yyyyMMddHHmm". 
117          */
118         public void build() {
119                 vid = buildQualifier(vid);
120                 this.qualifier = vid.getVersion().getSegment(3).toString();
121         }
122         
123         public VersionedId getVid() {
124                 return vid;
125         }
126         
127         public Version getVersion() {
128                 return vid.getVersion();
129         }
130         
131 }
132