001: package org.osbl.issue.gui;
002:
003: import org.osbl.client.wings.shell.*;
004: import org.osbl.client.wings.XButton;
005: import org.osbl.client.wings.XOptionPane;
006: import org.osbl.client.wings.form.*;
007: import org.osbl.client.ClientResourceProvider;
008: import org.osbl.issue.model.Issue;
009: import org.osbl.issue.model.Link;
010: import org.osbl.*;
011: import org.osbl.persistence.*;
012: import org.osbl.persistence.model.Entity;
013: import org.wings.*;
014: import org.wings.session.SessionManager;
015: import org.wings.border.SEmptyBorder;
016: import org.conform.wings.ComponentFactory;
017: import org.conform.*;
018: import org.conform.modifier.InlineModifier;
019: import org.conform.format.Format;
020: import org.conform.hibernate.HibernateEnvironment;
021: import org.concern.*;
022: import org.concern.controller.LoaderFactory;
023: import org.wingx.XDivision;
024:
025: import java.awt.event.ActionListener;
026: import java.awt.event.ActionEvent;
027: import java.awt.*;
028: import java.util.*;
029: import java.util.List;
030: import java.net.URLEncoder;
031: import java.io.UnsupportedEncodingException;
032:
033: public class IssuingTool extends AbstractTool implements FormTool {
034: private XDivision panel;
035: private ObjectForm objectForm;
036: private Persistence persistence;
037: private ObjectLocators objectLocators;
038:
039: public IssuingTool() {
040: putValue(Tool.NAME, Client.getInstance().getResourceProvider()
041: .getMessage("tool.issuing"));
042: }
043:
044: public void setForm(ObjectForm objectForm) {
045: this .objectForm = objectForm;
046: putValue(Tool.KEY, "Issuing: " + objectForm.getType().getName());
047: }
048:
049: public SComponent getComponent() {
050: if (panel == null) {
051: panel = new XDivision(new SGridBagLayout());
052: panel.setTitle(Client.getInstance().getResourceProvider()
053: .getMessage(Issue.class.getName()));
054: panel.setBorder(new SEmptyBorder(8, 8, 8, 8));
055: panel.setPreferredSize(SDimension.FULLWIDTH);
056:
057: final BeanMeta meta = new VariationBeanMetaProvider(Client
058: .getInstance().getBeanMetaProvider()).addModifier(
059: new InlineModifier() {
060: protected void configure() {
061: /*
062: property("key").setInitializer(new Initializer() {
063: public Object getValue() {
064: return format(objectForm.getObject());
065: }
066: });
067: */
068: property("assignee").setInitializer(
069: new Initializer() {
070: public Object getValue() {
071: return SessionManager
072: .getSession()
073: .getServletRequest()
074: .getUserPrincipal()
075: .getName();
076: }
077: });
078: }
079: }).getBeanMeta(Issue.class);
080: final BeanData data = new DefaultBeanData(meta);
081: createInstance(data);
082:
083: SComponent key = ComponentFactory.INSTANCE.createComponent(
084: meta, data, "name");
085: SComponent assignee = ComponentFactory.INSTANCE
086: .createComponent(meta, data, "assignee");
087: SComponent due = ComponentFactory.INSTANCE.createComponent(
088: meta, data, "due");
089: SComponent description = ComponentFactory.INSTANCE
090: .createComponent(meta, data, "description");
091:
092: XButton button = new XButton(Client.getInstance()
093: .getResourceProvider().getMessage(
094: "businessobject.buttons.create"));
095: button.addActionListener(new CreateAction(data));
096: button.setHorizontalAlignment(SConstants.RIGHT_ALIGN);
097:
098: key.setPreferredSize(SDimension.FULLWIDTH);
099: assignee.setPreferredSize(SDimension.FULLWIDTH);
100: due.setPreferredSize(SDimension.FULLWIDTH);
101: description.setPreferredSize(SDimension.FULLWIDTH);
102:
103: key.setFocusTraversalIndex(100);
104: assignee.setFocusTraversalIndex(101);
105: due.setFocusTraversalIndex(102);
106: description.setFocusTraversalIndex(103);
107: button.setFocusTraversalIndex(104);
108:
109: GridBagConstraints constraints = new GridBagConstraints();
110: constraints.insets = new Insets(0, 0, 4, 8);
111: constraints.gridwidth = GridBagConstraints.RELATIVE;
112: panel.add(ComponentFactory.INSTANCE.createLabel(meta,
113: "name"), constraints);
114: constraints.gridwidth = GridBagConstraints.REMAINDER;
115: panel.add(key, constraints);
116: constraints.gridwidth = GridBagConstraints.RELATIVE;
117: panel.add(ComponentFactory.INSTANCE.createLabel(meta,
118: "assignee"), constraints);
119: constraints.gridwidth = GridBagConstraints.REMAINDER;
120: panel.add(assignee, constraints);
121: constraints.gridwidth = GridBagConstraints.RELATIVE;
122: panel.add(ComponentFactory.INSTANCE
123: .createLabel(meta, "due"), constraints);
124: constraints.gridwidth = GridBagConstraints.REMAINDER;
125: panel.add(due, constraints);
126: panel.add(description, constraints);
127:
128: constraints.insets = new Insets(12, 0, 2, 8);
129: panel.add(button, constraints);
130: }
131: return panel;
132: }
133:
134: private void createInstance(BeanData data) {
135: data.setValue(data.createInstance());
136: data.initialize();
137: }
138:
139: private String query(Object object) {
140: Class type = objectForm.getType();
141: ObjectLocator objectLocator = getObjectLocators().get(type);
142: if (objectLocator == null)
143: return null;
144:
145: Map<String, String> query = objectLocator.queryByObject(object);
146:
147: StringBuilder builder = new StringBuilder();
148: try {
149: builder.append('?');
150: builder.append("edit=");
151: builder.append(type.getName());
152:
153: for (Map.Entry<String, String> entry : query.entrySet()) {
154: builder.append('&');
155: builder.append(entry.getKey());
156: builder.append('=');
157: builder.append(URLEncoder.encode(entry.getValue(),
158: "UTF-8"));
159: }
160: } catch (UnsupportedEncodingException e) {
161: throw new RuntimeException(e); // won't happen
162: }
163:
164: return builder.toString();
165: }
166:
167: public String format(Object object) throws LoaderException {
168: Client client = Client.getInstance();
169: ClientResourceProvider resourceProvider = client
170: .getResourceProvider();
171: BeanMetaProvider beanMetaProvider = client
172: .getBeanMetaProvider();
173: BeanMeta beanMeta = beanMetaProvider.getBeanMeta(objectForm
174: .getType());
175:
176: String patternKey = (String) beanMeta
177: .getAttribute(PropertyMeta.ATTRIBUTE_PATTERN_KEY);
178: Format subjectFormat = beanMeta.getFormat();
179:
180: if (patternKey != null)
181: subjectFormat.setPattern(resourceProvider.getMessage(client
182: .getLocale(), patternKey));
183:
184: return subjectFormat.format(object);
185: }
186:
187: private void save(Issue issue) {
188: SaveOrUpdateCommand command = (SaveOrUpdateCommand) getCommandFactory()
189: .createCommand("save");
190: command.setObject(issue);
191: command.execute();
192: }
193:
194: public Persistence getCommandFactory() {
195: if (persistence == null)
196: persistence = (Persistence) ServiceProvider.getInstance()
197: .getService("IssuePersistence");
198: return persistence;
199: }
200:
201: public ObjectLocators getObjectLocators() {
202: if (objectLocators == null)
203: objectLocators = (ObjectLocators) ServiceProvider
204: .getInstance().getService("ObjectLocators");
205: return objectLocators;
206: }
207:
208: private class CreateAction implements ActionListener {
209: private final BeanData data;
210:
211: public CreateAction(BeanData data) {
212: this .data = data;
213: }
214:
215: public void actionPerformed(ActionEvent event) {
216: Object object = objectForm.getObject();
217:
218: if (objectForm.hasChanges() || unsavedEntity(object)) {
219: XOptionPane.showMessageDialog(panel, Client
220: .getInstance().getResourceProvider()
221: .getMessage(
222: Issue.class.getName()
223: + ".messages.unsaved"), null,
224: XOptionPane.WARNING_MESSAGE,
225: new ActionListener() {
226: public void actionPerformed(ActionEvent e) {
227: }
228: });
229: return;
230: }
231:
232: Issue issue = (Issue) data.getValue();
233: if (issue.getName() == null || issue.getDue() == null) {
234: XOptionPane.showMessageDialog(panel, Client
235: .getInstance().getResourceProvider()
236: .getMessage(
237: Issue.class.getName()
238: + ".messages.titleAndDue"),
239: null, XOptionPane.WARNING_MESSAGE,
240: new ActionListener() {
241: public void actionPerformed(ActionEvent e) {
242: }
243: });
244: return;
245: }
246:
247: issue.setKey(format(object));
248:
249: String string = query(object);
250: if (string != null) {
251: List<Link> links = new ArrayList<Link>();
252: Link link = new Link(format(object), string);
253: links.add(link);
254: issue.setLinks(links);
255: }
256:
257: Controller controller = ControllerLookup.getInstance()
258: .getController("Issue");
259: try {
260: HibernateEnvironment.getInstance().beginTransaction();
261: save(issue);
262:
263: Map annotations = new HashMap();
264: annotations.put("user", SessionManager.getSession()
265: .getServletRequest().getUserPrincipal()
266: .getName());
267:
268: Loader loader = new LoaderFactory()
269: .getLoader(controller);
270: String id = loader.idOf(issue);
271:
272: controller.createSubject(id);
273: controller.announceSubject(id);
274:
275: XOptionPane.showMessageDialog(panel, Client
276: .getInstance().getResourceProvider()
277: .getMessage(
278: Issue.class.getName()
279: + ".messages.created"), null,
280: XOptionPane.WARNING_MESSAGE,
281: new ActionListener() {
282: public void actionPerformed(ActionEvent e) {
283: createInstance(data);
284: }
285: });
286: } catch (Exception e) {
287: e.printStackTrace();
288: } finally {
289: HibernateEnvironment.getInstance().endTransaction();
290: }
291: }
292: }
293:
294: private boolean unsavedEntity(Object object) {
295: if (object instanceof Entity) {
296: Entity entity = (Entity) object;
297: return entity.getId() == null;
298: }
299: return false;
300: }
301: }
|