View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ProjectStore.java
13   * Created: 2007-00-00
14   * Author: klewandowski
15   * $Id: ProjectStore.java 3269 2009-08-28 11:22:09Z tmilos $
16   */
17  
18  package pl.edu.agh.cast.backward.resources;
19  
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.TreeMap;
24  import java.util.Map.Entry;
25  
26  import org.apache.log4j.Logger;
27  import org.eclipse.core.resources.IProject;
28  
29  import pl.edu.agh.cast.Activator;
30  import pl.edu.agh.cast.model.base.IModel;
31  import pl.edu.agh.cast.model.base.Model;
32  import pl.edu.agh.cast.model.mapper.Mapper;
33  import pl.edu.agh.cast.project.ProjectUtil;
34  
35  /**
36   * Used to store models during one session.
37   *
38   * @author AGH CAST Team
39   */
40  public final class ProjectStore {
41  
42  	private static ProjectStore instance = new ProjectStore();
43  
44  	private static Logger log = Activator.getLogger();
45  
46  	/**
47  	 * Project models.
48  	 */
49  	private Map<IProject, IModel> projectToModel = new HashMap<IProject, IModel>();
50  
51  	private ProjectStore() {
52  	}
53  
54  	/**
55  	 * Returns single, shared instance of {@link ProjectStore}.
56  	 *
57  	 * @return single, shared instance of {@link ProjectStore}
58  	 */
59  	public static ProjectStore getInstance() {
60  		return instance;
61  	}
62  
63  	/**
64  	 * Checks if project is initialized.
65  	 *
66  	 * @param project
67  	 *            project to check
68  	 * @return <code>true</code> if project is present in the {@link ProjectStore}
69  	 */
70  	public boolean isInitialized(IProject project) {
71  		return projectToModel.containsKey(project);
72  	}
73  
74  	/**
75  	 * Initialize the project.
76  	 *
77  	 * If the project has property "id" set, then the procedure tries to find model with the same id in the DB. If there
78  	 * is no such model then it is created (and stored in the DB).
79  	 *
80  	 * If there are more than one model, then new default model is instantiated, but is not stored in the DB. The same
81  	 * happens if any other problem occurs (especially, when the DB system is not configured properly).
82  	 *
83  	 * @param project
84  	 *            project to initialize
85  	 */
86  	@SuppressWarnings("unchecked")
87  	public void init(IProject project) {
88  		IModel model = null;
89  		String newId = null;
90  		TreeMap<String, Object> conditions = new TreeMap<String, Object>();
91  		try {
92  			String id = ProjectUtil.getProperty(project, ProjectUtil.PROJECT_ID_PROPERTY);
93  			if (id == null) {
94  				newId = ProjectUtil.getInstance().generateProjectId();
95  				id = newId;
96  			}
97  
98  			conditions.put("getName", id); //$NON-NLS-1$
99  			List<Model> models = Mapper.getInstance().find(Model.class, null, conditions, project);
100 
101 			if (models.isEmpty()) {
102 				model = createNewModel(id, project);
103 			} else {
104 				if (models.size() == 1) {
105 					model = models.get(0);
106 				} else {
107 					throw new RuntimeException("Too many models with id " + id); //$NON-NLS-1$
108 				}
109 			}
110 		} catch (Exception ex) {
111 			log.error("Failed to load/save project model. Using default.", ex); //$NON-NLS-1$
112 			model = new Model(ProjectUtil.getInstance().generateProjectId());
113 		} finally {
114 			projectToModel.put(project, model);
115 
116 			if (newId != null) {
117 				ProjectUtil.setProperty(project, ProjectUtil.PROJECT_ID_PROPERTY, newId);
118 			}
119 		}
120 	}
121 
122 	/**
123 	 * Creates new model and stores it in the DB.
124 	 *
125 	 * @param id
126 	 *            the id of the model
127 	 * @return new model instance
128 	 */
129 	private IModel createNewModel(String id, IProject project) {
130 		IModel model = new Model(id);
131 		Mapper.getInstance().save(model, project);
132 		return model;
133 	}
134 
135 	/**
136 	 * Returns model for given project.
137 	 *
138 	 * @param project
139 	 *            the project for which the model is returned
140 	 * @return the model associated with the project or <code>null</code> if no model is associated with the project
141 	 */
142 	public IModel getModel(IProject project) {
143 		if (!isInitialized(project)) {
144 			init(project);
145 		}
146 		return projectToModel.get(project);
147 	}
148 
149 	/**
150 	 * Returns {@link IProject} for given {@link IModel}.
151 	 *
152 	 * @param model
153 	 *            the model associated with the project
154 	 * @return the project associated with the model
155 	 */
156 	public IProject getProject(IModel model) {
157 		for (Entry<IProject, IModel> entry : projectToModel.entrySet()) {
158 			if (entry.getValue().equals(model)) {
159 				return entry.getKey();
160 			}
161 		}
162 		return null;
163 	}
164 
165 	/**
166 	 * Removes the given project.
167 	 *
168 	 * @param project
169 	 *            project to remove
170 	 */
171 	public void remove(IProject project) {
172 		if (isInitialized(project)) {
173 			projectToModel.remove(project);
174 		}
175 	}
176 }