001: package org.osbl.persistence.hibernate;
002:
003: import org.osbl.persistence.SaveOrUpdateCommand;
004: import org.osbl.persistence.SaveOrUpdateGeneralCommand;
005: import org.osbl.persistence.model.General;
006: import org.osbl.persistence.model.Concrete;
007: import org.osbl.ServiceProvider;
008: import org.osbl.plugin.*;
009: import org.hibernate.*;
010:
011: import java.util.*;
012:
013: public class HibernateSaveOrUpdateGeneralCommand<T> extends
014: SaveOrUpdateGeneralCommand implements HibernateCommand {
015: SessionFactory sessionFactory;
016:
017: public void setSessionFactory(SessionFactory sessionFactory) {
018: this .sessionFactory = sessionFactory;
019: }
020:
021: public void execute() {
022: Session session = sessionFactory.getCurrentSession();
023:
024: General<?> general = (General<?>) object;
025: session.saveOrUpdate(general);
026:
027: PluginManager pluginManager = (PluginManager) ServiceProvider
028: .getInstance().getService("PluginManager");
029: ExtensionPoint extensionPoint = new ExtensionPoint(Hibernate
030: .getClass(general).getName()
031: + ".concrete", Concrete.class);
032: List<Extension> list = pluginManager
033: .getExtensions(extensionPoint);
034: List<String> defined = defined(list);
035: List<String> loaded = loaded(general);
036: List<String> qualified = qualified(general);
037:
038: List<Concrete> storedConcretes = new ArrayList<Concrete>();
039: List<String> stored = new ArrayList<String>();
040:
041: List<? extends Concrete> generalConcretes = general
042: .getConcretes();
043: if (generalConcretes == null)
044: generalConcretes = Collections.EMPTY_LIST;
045: else {
046: generalConcretes = new ArrayList(generalConcretes);
047: general.getConcretes().clear();
048: }
049:
050: for (Extension extension : list) {
051: Concrete concrete = (Concrete) session.get(extension
052: .getImplementation(), general.getId());
053: if (concrete != null) {
054: storedConcretes.add(concrete);
055: stored.add(extension.getImplementation()
056: .getSimpleName());
057: }
058: }
059:
060: System.out.println("defined " + defined);
061: System.out.println("loaded " + loaded);
062: System.out.println("qualified " + qualified);
063: System.out.println("stored " + stored);
064:
065: List<Concrete> save = new ArrayList<Concrete>(qualified.size());
066: List<Concrete> update = new ArrayList<Concrete>(qualified
067: .size());
068: List<Concrete> delete = new ArrayList<Concrete>(defined.size());
069:
070: for (Concrete concrete : generalConcretes) {
071: String name = concrete.getClass().getSimpleName();
072: if (qualified.contains(name)) {
073: if (stored.contains(name)) {
074: update.add(concrete);
075: System.out.println("... update " + name);
076: } else {
077: save.add(concrete);
078: System.out.println("... add " + name);
079: }
080: }
081: }
082:
083: for (Concrete concrete : storedConcretes) {
084: String name = concrete.getClass().getSimpleName();
085: if (!qualified.contains(name)) {
086: delete.add(concrete);
087: general.getConcretes().remove(concrete);
088: System.out.println("... delete " + name);
089: }
090: }
091:
092: session.saveOrUpdate(general);
093:
094: for (Concrete concrete : save) {
095: if (concrete.getGeneral() == null)
096: concrete.setGeneral(general);
097: session.save(concrete);
098: }
099: for (Concrete concrete : update) {
100: if (concrete.getGeneral() == null)
101: concrete.setGeneral(general);
102: session.merge(concrete);
103: }
104: for (Concrete concrete : delete) {
105: session.delete(concrete);
106: }
107: }
108:
109: private List<String> defined(List<Extension> extensions) {
110: List<String> list = new ArrayList<String>(extensions.size());
111: for (Extension extension : extensions) {
112: list.add(extension.getImplementation().getSimpleName());
113: }
114: return list;
115: }
116:
117: private List<String> qualified(General general) {
118: String qualifications = general.getQualifications();
119: if (qualifications == null)
120: qualifications = "";
121:
122: String[] strings = qualifications.split("\\|");
123: List<String> list = new ArrayList<String>(strings.length);
124: for (String string : strings) {
125: list.add(string);
126: }
127: if (list.size() > 1)
128: list.remove(0);
129:
130: return list;
131: }
132:
133: private List<String> loaded(General general) {
134: List concretes = general.getConcretes();
135: if (concretes == null)
136: return Collections.EMPTY_LIST;
137:
138: List<String> list = new ArrayList<String>(concretes.size());
139: for (Object concrete : concretes) {
140: list.add(concrete.getClass().getSimpleName());
141: }
142: return list;
143: }
144:
145: private List<String> stored(Session session, List<Extension> list,
146: General general) {
147: List<String> peeks = new LinkedList<String>();
148: for (Extension extension : list) {
149: if (session.get(extension.getImplementation(), general
150: .getId()) != null)
151: peeks
152: .add(extension.getImplementation()
153: .getSimpleName());
154: }
155: return peeks;
156: }
157: }
|