001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.services.config;
009:
010: //base classes
011: import java.io.ByteArrayInputStream;
012: import java.io.ByteArrayOutputStream;
013: import java.io.IOException;
014: import java.io.OutputStreamWriter;
015: import java.math.BigDecimal;
016: import java.sql.Connection;
017: import java.sql.SQLException;
018:
019: //project specific classes
020: import org.jfolder.common.UnexpectedSystemException;
021: import org.jfolder.common.tagging.ConceptRollupTag;
022: import org.jfolder.common.tagging.ConceptTagCharacteristic;
023: import org.jfolder.common.tagging.ConceptTagCharacteristicHolder;
024: import org.jfolder.common.tagging.ConceptTagPreferences;
025: import org.jfolder.common.tagging.ConceptTagSetContext;
026: import org.jfolder.common.tagging.ConceptTagSetHolder;
027: import org.jfolder.common.tagging.RootConceptTagHolder;
028: import org.jfolder.config.instance.ConfigInstance;
029: import org.jfolder.config.instance.ConfigInstanceConfig;
030: import org.jfolder.config.instance.ConfigInstanceTagHelper;
031: import org.jfolder.platforms.stores.base.CreateStatement;
032: import org.jfolder.platforms.stores.base.SelectStatement;
033: import org.jfolder.platforms.stores.base.SystemDatabase;
034: import org.jfolder.platforms.stores.base.SystemResultSet;
035: import org.jfolder.platforms.stores.base.SystemStatement;
036: import org.jfolder.platforms.stores.base.SystemStore;
037: import org.jfolder.platforms.stores.base.SystemStoreFactory;
038: import org.jfolder.platforms.stores.base.UpdateStatement;
039: import org.jfolder.platforms.stores.base.WhereStatement;
040: import org.jfolder.services.base.AbstractBaseServiceBean;
041:
042: //other classes
043:
044: class ConfigServiceBean extends AbstractBaseServiceBean implements
045: ConfigService {
046:
047: private RootConceptTagHolder rcth = null;
048:
049: protected ConfigServiceBean() {
050: }
051:
052: //
053: private synchronized void load(boolean inReset) {
054: try {
055: if (inReset) {
056: this .rcth = null;
057: }
058: if (this .rcth == null) {
059: ByteArrayOutputStream baos = new ByteArrayOutputStream();
060: OutputStreamWriter osw = new OutputStreamWriter(baos);
061: osw.write(SystemStore.getConfig());
062: osw.flush();
063: baos.flush();
064: ByteArrayInputStream bais = new ByteArrayInputStream(
065: baos.toByteArray());
066: this .rcth = ConfigInstanceTagHelper.getRootHolder(bais);
067: }
068: } catch (IOException ioe) {
069: throw new UnexpectedSystemException(ioe);
070: }
071: }
072:
073: //
074: public RootConceptTagHolder getCopyOfCurrentConfigInstance() {
075:
076: RootConceptTagHolder outValue = null;
077:
078: load(false);
079:
080: byte content[] = ConceptRollupTag.getRollupFromList(this .rcth,
081: null);
082: outValue = ConceptRollupTag.getRootHolderFromList(content);
083:
084: return outValue;
085: }
086:
087: public void setCurrentConfigInstance(RootConceptTagHolder inRcth) {
088:
089: Connection conn = null;
090:
091: try {
092:
093: //
094: SystemStore ss = SystemStoreFactory.getSystemStore();
095: conn = SystemStore.getConnection(ss);
096:
097: //String s = inRcth.getXMLRepresentation();
098:
099: //
100: CreateStatement systemTable = SystemDatabase.newInstance()
101: .createSystemTable();
102: SelectStatement systemSelect = SelectStatement
103: .createOrderById(systemTable, null);
104: //
105: ss.execute(systemSelect, conn);
106: SystemResultSet systemResults = systemSelect.getResults();
107: //
108: BigDecimal systemId = systemResults.getDecimal(0, 0);
109:
110: //
111: UpdateStatement systemUpdate = SystemStatement
112: .newUpdateStatement(systemTable);
113: systemUpdate.addWhereColumn(null,
114: SystemStatement.ID_COLUMN, systemTable, systemId,
115: WhereStatement.EQUALS, null);
116: systemUpdate.addUpdateColumn(
117: SystemDatabase.C_CONFIG_T_SYSTEM, inRcth
118: .getXMLRepresentation());
119: //
120: ss.execute(systemUpdate, conn);
121:
122: //
123: ss.commit(conn);
124:
125: //
126: SystemStore.initialize();
127: load(true);
128:
129: } finally {
130: SystemStore.closeSQLObjects(conn, null, null);
131: }
132: }
133:
134: public void resetCurrentConfig() {
135:
136: setCurrentConfigInstance(ConfigInstanceTagHelper.getDefault());
137: //Connection conn = null;
138: //
139: //try {
140: //}
141: //finally {
142: // SystemStore.closeSQLObjects(conn, null, null);
143: //}
144: }
145:
146: //
147: public ConceptTagSetHolder accessConceptTagSet(String inNamespace,
148: ConceptTagSetContext inCtsc) {
149:
150: ConceptTagSetHolder outValue = null;
151:
152: outValue = getConfigInstance().accessConceptTagSet(inNamespace,
153: inCtsc);
154:
155: return outValue;
156: }
157:
158: //
159: public ConfigInstanceConfig accessConfig(String inNamespace) {
160:
161: ConfigInstanceConfig outValue = null;
162:
163: outValue = getConfigInstance().accessConfig(inNamespace);
164:
165: return outValue;
166: }
167:
168: //
169: private ConfigInstance getConfigInstance() {
170:
171: ConfigInstance outValue = null;
172:
173: load(false);
174: ConceptTagPreferences ctp = this .rcth.getPreferences();
175: ConceptTagCharacteristicHolder ctcharh = this .rcth
176: .getRootCharacteristicHolder();
177: ConceptTagCharacteristic ctchar = ctcharh.getCharacteristic(ctp
178: .getRootConstraint());
179: ConceptTagCharacteristicHolder ctCharh = ctchar
180: .getValueAsHolder(0, null);
181: outValue = (ConfigInstance) ctCharh.getConceptTag();
182:
183: return outValue;
184: }
185:
186: }
|