Created the ContentModifyingProducerNode
authorzapata <zapata>
Sat, 6 Jul 2002 01:37:38 +0000 (01:37 +0000)
committerzapata <zapata>
Sat, 6 Jul 2002 01:37:38 +0000 (01:37 +0000)
source/mircoders/producer/ContentModifyingProducerNode.java [new file with mode: 0755]

diff --git a/source/mircoders/producer/ContentModifyingProducerNode.java b/source/mircoders/producer/ContentModifyingProducerNode.java
new file mode 100755 (executable)
index 0000000..0939b2e
--- /dev/null
@@ -0,0 +1,65 @@
+package mircoders.producer;
+
+import java.util.*;
+import java.io.*;
+import mir.util.*;
+import mir.producer.*;
+import mir.entity.*;
+import mir.entity.adapter.*;
+import mircoders.entity.*;
+import mircoders.module.*;
+import mircoders.storage.*;
+
+
+public class ContentModifyingProducerNode implements ProducerNode {
+  private String contentKey;
+  private String fieldNameExpression;
+  private String valueExpression;
+
+  public ContentModifyingProducerNode(String aContentKey, String aFieldNameExpression, String aValueExpression) {
+    contentKey = aContentKey;
+    fieldNameExpression = aFieldNameExpression;
+    valueExpression = aValueExpression;
+  }
+
+  public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
+    Object data;
+    Entity entity;
+    String value;
+    String fieldName;
+
+    try {
+      ModuleContent contentModule = new ModuleContent(DatabaseContent.getInstance());
+
+      data = ParameterExpander.findValueForKey( aValueMap, contentKey );
+
+      if (! (data instanceof EntityAdapter)) {
+        throw new ProducerFailure("ContentModifyingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
+      }
+
+      entity = ((EntityAdapter) data).getEntity();
+      if (! (entity instanceof EntityContent)) {
+        throw new ProducerFailure("ContentModifyingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
+      }
+
+      value = ParameterExpander.expandExpression(aValueMap, valueExpression);
+      fieldName = ParameterExpander.expandExpression(aValueMap, fieldNameExpression);
+
+      entity.setValueForProperty("is_produced", "0");
+      entity.setValueForProperty(fieldName, value);
+      entity.update();
+
+      aLogger.println("  Modified content " + entity.get("id") + ": " + fieldName + " = " + value );
+    }
+    catch (Throwable t) {
+      aLogger.println("Error while modifying content: " + t.getMessage());
+      t.printStackTrace(aLogger);
+
+      throw new ProducerFailure(t.getMessage(), t);
+    }
+  }
+
+  public Set buildVerbSet() {
+    return new HashSet();
+  }
+}