List of usage examples for org.apache.commons.lang.time DurationFormatUtils formatDuration
public static String formatDuration(long durationMillis, String format)
Formats the time gap as a string, using the specified format, and padding with zeros and using the default timezone.
This method formats durations using the days and lower fields of the format pattern.
From source file:smilehouse.opensyncro.servlets.SyncroEditPipe.java
public String handleRequest(HttpServletRequest req, HttpSession session, ParameterManager parameters, Persister pers, LabelResource labels, int requestType) { this.pipe = getPipe(parameters, session, pers); if (this.pipe == null) return ""; //Field nameField = nameFieldInfo.getField(pipe, labels, req); Field converterField = converterFieldInfo.getField(pipe, labels, req); //Field verbosityLevelField = verbosityLevelFieldInfo.getField(pipe, labels, req); java.util.Map generalFields = generalFieldsContext.makeFields(pipe, labels, req); // ----------- // Handle post // ----------- if (requestType == POST_REQUEST) { if (parameters.getInt(ACTION) != ACTION_CANCEL) { // ----------------------------------------- // Commit fields if somebody has edited them // ----------------------------------------- // if(nameField.hasBeenEdited() && nameField.isEditValid()) { // nameField.commit(); // } if (converterField.hasBeenEdited() && converterField.isEditValid()) { converterField.commit(); }// w ww . j av a 2 s .c o m // if(verbosityLevelField.hasBeenEdited() && verbosityLevelField.isEditValid()) { // verbosityLevelField.commit(); // } generalFieldsContext.commitFields(generalFields); // --------------------------- // Create components if needed // --------------------------- if (parameters.wasGiven(CREATE_COMPONENT_OF_TYPE) && parameters.getInt(CREATE_COMPONENT_OF_TYPE) != -1) { // Find out the class name of the component to be created int componentType = parameters.getInt(CREATE_COMPONENT_OF_TYPE); String className = req.getParameter("compImpl" + componentType); if (className != null) { // Load the implementation description, create component instance and put it // into it's place in the pipe //PipeComponentImplementation impl = pers.loadComponentImplementation(className); PipeComponentIF impl = Persister.getInstance(className); if (impl != null) { //PipeComponentIF newComponent = impl.getInstance(); PipeComponentIF newComponent = Persister.getInstance(className); PipeComponentData pcdata; pcdata = new PipeComponentData(); pcdata.setAttributes(new HashMap()); //Long dataId; switch (componentType) { case PipeComponentIF.TYPE_SOURCE: pipe.setSource((SourceIF) newComponent); pipe.setSourceID(newComponent.getID()); pcdata = new PipeComponentData(); pipe.setSourceData(pcdata); newComponent.setData(pcdata); pers.save(pcdata); //dataId = pcdata.getId(); break; case PipeComponentIF.TYPE_DESTINATION: pipe.setDestination((DestinationIF) newComponent); pipe.setDestinationID(newComponent.getID()); pcdata = new PipeComponentData(); pipe.setDestinationData(pcdata); newComponent.setData(pcdata); pers.save(pcdata); //dataId = pcdata.getId(); break; case PipeComponentIF.TYPE_CONVERTER: pcdata = new PipeComponentData(); ConverterListItem cl = pipe.addConverter((ConverterIF) newComponent, pcdata); pers.save(cl); pers.save(pipe); //dataId = pcdata.getId(); /*System.out.println("Created new Converter (" + newComponent.getID() + ", " + className + "), dataId: " + dataId);*/ converterField = converterFieldInfo.getField(pipe, labels, req); break; } } } } // ------ // Delete // ------ if (parameters.wasGiven(DELETE_COMPONENT_OF_TYPE) && parameters.getInt(DELETE_COMPONENT_OF_TYPE) != -1) { switch (parameters.getInt(DELETE_COMPONENT_OF_TYPE)) { case PipeComponentIF.TYPE_SOURCE: PipeComponentIF source = pipe.getCurrentSource(); String sourceID = pipe.getSourceID(); if (sourceID != null) { PipeComponentData sourceData = this.pipe.getSourceData(); // We need to remove references to Source component's PipeComponentData before deleting it if (source != null) { source.setData(null); } pipe.setSourceData(null); // Delete the Source component's PipeComponentData pers.delete(sourceData); // Clear Source component instance and ID pipe.setSource(null); pipe.setSourceID(null); } break; case PipeComponentIF.TYPE_DESTINATION: PipeComponentIF destination = pipe.getCurrentDestination(); String destinationID = pipe.getDestinationID(); if (destinationID != null) { PipeComponentData destinationData = this.pipe.getDestinationData(); // We need to remove references to Destination component's PipeComponentData before deleting it if (destination != null) { destination.setData(null); } pipe.setDestinationData(null); // Delete the Destination component's PipeComponentData pers.delete(destinationData); // Clear Source component instance and ID pipe.setDestination(null); pipe.setDestinationID(null); } break; } } if (parameters.getInt(ACTION) == ACTION_START) { pipe.transfer(new TransferInfo(pers.getDatabaseName(), (String) session.getAttribute(StandaloneEnvironment.SESSION_USER_ATTR))); } pers.update(pipe); } //else // nameField.revert(); } //String nameHtml = nameField.getEditor(); //String verbosityLevelHtml = verbosityLevelField.getEditor(); /** Create TD for Source Component */ TD sourceCell = null; SourceIF pipeSourceComponent = pipe.getSource(); if ((pipeSourceComponent == null) && (pipe.getSourceID() != null)) { // Source component which cannot be loaded sourceCell = getUnknownComponentCell(pipe.getSourceID(), pers, PipeComponentIF.TYPE_SOURCE, labels); } else { sourceCell = getComponentCell(pipeSourceComponent, pers, PipeComponentIF.TYPE_SOURCE, labels); } /** Create TD for Destination Component */ TD destinationCell = null; DestinationIF pipeDestinationComponent = pipe.getDestination(); if ((pipeDestinationComponent == null) && (pipe.getDestinationID() != null)) { // Destination component which cannot be loaded destinationCell = getUnknownComponentCell(pipe.getDestinationID(), pers, PipeComponentIF.TYPE_DESTINATION, labels); } else { destinationCell = getComponentCell(pipeDestinationComponent, pers, PipeComponentIF.TYPE_DESTINATION, labels); } /** TODO: If Converter component is unloadable, Converter list should display label "converter_component_unloadable" (+ConverterID) instead of 'null' */ TD convertersCell = getConvertersCell(converterField, pers, labels); Table componentTable = new Table().setBorder(0).setCellSpacing(0).setCellPadding(5).addElement(new TR() .addElement(new TD().addElement(new B(labels.getLabel("source"))).setClass("ttheader1")) .addElement(new TD().setRowSpan(2).setVAlign(AlignType.MIDDLE).addElement(new B("-->"))) .addElement(new TD().addElement(new B(labels.getLabel("converters"))).setClass("ttheader1")) .addElement(new TD().setRowSpan(2).setVAlign(AlignType.MIDDLE).addElement(new B("-->"))) .addElement(new TD().addElement(new B(labels.getLabel("destination"))).setClass("ttheader1"))) .addElement(new TR().setVAlign(AlignType.TOP).addElement(sourceCell.setBgColor("FFFFFF")) .addElement(convertersCell.setBgColor("FFFFFF")) .addElement(destinationCell.setBgColor("FFFFFF"))); // -------------------------- // A button to start the pipe // -------------------------- Input startButton = new Input(Input.BUTTON, "strt", labels.getLabel("start")); startButton.setOnClick( "document.forms[0]." + ACTION + ".value=" + ACTION_START + ";document.forms[0].submit()"); Table infoTable = new Table().setBorder(0).setCellSpacing(0).setCellPadding(5) .addElement(new TR().addElement(new TD().addElement(startButton))); //Add pipe execution information after pipe execution if (parameters.getInt(ACTION) == ACTION_START) { infoTable .addElement(new TD().setRowSpan(1).setVAlign(AlignType.MIDDLE) .addElement(new B(labels.getLabel("starttimeeditor") + ":"))) .addElement(new TD().setRowSpan(2).setVAlign(AlignType.MIDDLE) .addElement(dateFormat.format(pipe.getStartTime()))) .addElement(new TD().setRowSpan(1).setVAlign(AlignType.MIDDLE) .addElement(new B(labels.getLabel("endtimeeditor") + ":"))) .addElement(new TD().setRowSpan(2).setVAlign(AlignType.MIDDLE) .addElement(dateFormat.format(pipe.getEndTime()))) .addElement(new TD().setRowSpan(1).setVAlign(AlignType.MIDDLE) .addElement(new B(labels.getLabel("editorduration") + ":"))) .addElement(new TD().setRowSpan(2).setVAlign(AlignType.MIDDLE) .addElement(DurationFormatUtils.formatDuration(pipe.getDuration(), DURATION_FORMAT))) .addElement(new TD().setRowSpan(1).setVAlign(AlignType.MIDDLE) .addElement(new B(labels.getLabel("editorstatus") + ":"))) .addElement( new TD().setRowSpan(2).setVAlign(AlignType.MIDDLE).addElement(pipe.getLastStatus())); } Template generalFieldsTemplate = Template.createTemplate(GENERAL_FIELDS_LAYOUT); generalFieldsContext.writeEditors(generalFields, generalFieldsTemplate); generalFieldsTemplate.write(); ElementContainer content = new ElementContainer().addElement(new H1(pipe.getName())) .addElement(generalFieldsTemplate.toString()).addElement(new BR()).addElement(new BR()) .addElement(componentTable).addElement(new BR()).addElement(infoTable); Form form = getForm("EditPipe", content.toString(), labels, false, true) .addElement(parameters.getInput(Input.HIDDEN, PIPE_ID)) .addElement(new Input(Input.HIDDEN, CREATE_COMPONENT_OF_TYPE, "-1")) .addElement(new Input(Input.HIDDEN, DELETE_COMPONENT_OF_TYPE, "-1")); return form.toString(); }
From source file:smilehouse.opensyncro.servlets.SyncroPipeList.java
public String handleRequest(HttpServletRequest req, HttpSession session, ParameterManager parameters, Persister pers, LabelResource labels, int requestType) { //Get executing pipes (started via HttpStart) from webapp context. //This allows executing pipes to be highlighted in the pipe list. Object pipesAndThreadsObject = this.getServletContext().getAttribute(SyncroHttpStart.PIPES_AND_THREADS); Map pipest = null;/*from w w w. j a v a2s.c om*/ final String language = environment.getLanguage(session); if (pipesAndThreadsObject != null) { pipest = (Map) this.getServletContext().getAttribute(SyncroHttpStart.PIPES_AND_THREADS); //Pipes are actually map keys. pipeSet = pipest.keySet(); } try { // Well, here goes for nothing... ModelModifier modifier = new DefaultModelModifier() { public Object getModelValue(Object model) throws Exception { return ((ListModel) model).getList(); } public void setModelValue(Object model, Object value) throws Exception { ((ListModel) model).setList((RecallingList) value); } }; HighlightingContainerEditor hlContainerEditor = new HighlightingContainerEditor(); hlContainerEditor.enableDelete("delete", "confirm_delete_pipe"); hlContainerEditor.enableClone(Pipe.class, "clone"); hlContainerEditor.enableAdd(Pipe.class, "add"); hlContainerEditor.setActivePipes(pipeSet); pipeListFieldInfo = new ContainerFieldInfo("list", "pipes", modifier, hlContainerEditor); { // Editor for Pipe name String id1 = "name"; String label1 = "name"; String property1 = "name"; PropertySetter propertySetter1 = new IntrospectionPropertySetter(Pipe.class); ModelModifier modifier1 = new PropertyBasedModelModifier(property1, propertySetter1); TextEditor editor1 = new TextEditor(); editor1.setSize(50); FieldInfo fieldInfo1 = new FieldInfo(id1, label1, modifier1, editor1); pipeListFieldInfo.addColumn(fieldInfo1); } { // ----------------- // Start time // ----------------- String id3 = "starttime"; String label3 = "starttimelist"; ModelModifier modifier3 = new DefaultModelModifier() { public Object getModelValue(Object model) throws Exception { Date start = ((Pipe) model).getStartTime(); if (start == null) { return ""; } return dateFormat.format(start); } public void setModelValue(Object model, Object value) throws Exception { } }; UneditingEditor editor3 = new UneditingEditor(); FieldInfo linkFieldInfo = new FieldInfo(id3, label3, modifier3, editor3); pipeListFieldInfo.addColumn(linkFieldInfo); } { // ----------------- // Finish time // ----------------- String id4 = "endtime"; String label4 = "endtimelist"; ModelModifier modifier4 = new DefaultModelModifier() { public Object getModelValue(Object model) throws Exception { Date end = ((Pipe) model).getEndTime(); if (end == null || (pipeSet != null && pipeSet.contains((Pipe) model))) { return ""; } return dateFormat.format(end); } public void setModelValue(Object model, Object value) throws Exception { } }; UneditingEditor editor4 = new UneditingEditor(); FieldInfo linkFieldInfo = new FieldInfo(id4, label4, modifier4, editor4); pipeListFieldInfo.addColumn(linkFieldInfo); } { // ----------------- // Duration of pipe execution // ----------------- String id4 = "duration"; String label4 = "duration"; ModelModifier modifier4 = new DefaultModelModifier() { public Object getModelValue(Object model) throws Exception { Long duration = ((Pipe) model).getDuration(); if (duration == 0) return ""; else if (pipeSet != null && pipeSet.contains((Pipe) model)) return environment.getLabel(PIPE_RUNNING, language); else return DurationFormatUtils.formatDuration(duration, DURATION_FORMAT); } public void setModelValue(Object model, Object value) throws Exception { } }; UneditingEditor editor4 = new UneditingEditor(); FieldInfo linkFieldInfo = new FieldInfo(id4, label4, modifier4, editor4); pipeListFieldInfo.addColumn(linkFieldInfo); } { // ----------------- // Last status // ----------------- String id4 = "status"; String label4 = "laststatus"; ModelModifier modifier4 = new DefaultModelModifier() { public Object getModelValue(Object model) throws Exception { String status = ((Pipe) model).getLastStatus(); if (status != null && (pipeSet == null || !pipeSet.contains((Pipe) model))) return status; return ""; } public void setModelValue(Object model, Object value) throws Exception { } }; UneditingEditor editor4 = new UneditingEditor(); FieldInfo linkFieldInfo = new FieldInfo(id4, label4, modifier4, editor4); pipeListFieldInfo.addColumn(linkFieldInfo); } { // ----------------- // Last user // ----------------- String id6 = "lastuser"; String label6 = "lastuser"; ModelModifier modifier6 = new DefaultModelModifier() { public Object getModelValue(Object model) throws Exception { String user = ((Pipe) model).getUser(); if (user != null && (pipeSet == null || !pipeSet.contains((Pipe) model))) return user; return ""; } public void setModelValue(Object model, Object value) throws Exception { } }; UneditingEditor editor6 = new UneditingEditor(); FieldInfo linkFieldInfo = new FieldInfo(id6, label6, modifier6, editor6); pipeListFieldInfo.addColumn(linkFieldInfo); } { // ----------------- // Edit button field // ----------------- String id2 = "edit"; String label2 = "edit"; ModelModifier modifier2 = new DefaultModelModifier() { public Object getModelValue(Object model) throws Exception { return ((Pipe) model).getId(); } public void setModelValue(Object model, Object value) throws Exception { // We don't want to change it, we just want too see it } }; // Just want to show it... LinkButtonEditor editor2 = new LinkButtonEditor(); editor2.setHref("EditPipe"); editor2.setParameterName(PIPE_ID); editor2.setTarget("_self"); FieldInfo linkFieldInfo = new FieldInfo(id2, label2, modifier2, editor2); pipeListFieldInfo.addColumn(linkFieldInfo); } { // ----------------- // Start button field // ----------------- String id5 = "start"; String label5 = "start"; ModelModifier modifier5 = new DefaultModelModifier() { public Object getModelValue(Object model) throws Exception { return ((Pipe) model).getId(); } public void setModelValue(Object model, Object value) throws Exception { } }; LinkButtonEditor editor5 = new LinkButtonEditor(); editor5.setHref("PipeList"); editor5.setParameterName(PIPE_ID); editor5.setTarget("_self"); FieldInfo linkFieldInfo = new FieldInfo(id5, label5, modifier5, editor5); pipeListFieldInfo.addColumn(linkFieldInfo); } } catch (Exception e) { environment.log("Problems initializing GUI: \n" + e.getMessage()); } ListModel pipes = new ListModel(pers.loadAllPipes()); Field listField = pipeListFieldInfo.getField(pipes, environment.getLabelResource(session), req); //Try to get pipe id from the request. Long pipeId = new Long(parameters.getLong(PIPE_ID)); if (pipeId != 0) { //If the start button of one of the pipes was pressed, execute the pipe Pipe startedPipe = pers.loadPipe(pipeId); startedPipe.transfer( new TransferInfo(pers.getDatabaseName(), (String) session.getAttribute("syncro.user"))); pers.update(startedPipe); } if (requestType == POST_REQUEST) { if (parameters.getInt(ACTION) == 1) { listField.commit(); for (Iterator added = pipes.getList().addedIterator(); added.hasNext();) { Pipe pipe = (Pipe) added.next(); pers.save(pipe); //System.out.println("Saved pipe #" + pipe.getId().toString()); //environment.log("Saved pipe #" + pipe.getId().toString()); } /*for(Iterator cloned = pipes.getList().clonedIterator(); cloned.hasNext();) { Pipe pipe = (Pipe) cloned.next(); pers.save(pipe); }*/ for (Iterator current = pipes.getList().iterator(); current.hasNext();) { Pipe pipe = (Pipe) current.next(); pers.update(pipe); //System.out.println("Updated pipe #" + pipe.getId().toString()); //environment.log("Updated pipe #" + pipe.getId().toString()); } for (Iterator removed = pipes.getList().removedIterator(); removed.hasNext();) { Pipe pipe = (Pipe) removed.next(); //System.out.println("Removing pipe #" + pipe.getId().toString()); //environment.log("Removing pipe #" + pipe.getId().toString()); pers.delete(pipe); } } else listField.revert(); } Form form = getForm("PipeList", listField.getEditor(), labels, false, true); return form.toString(); }