Sorry, I wasn't using JavaScript. I coded it in Java. For what it's worth, 
here's a pass at the code.

Note: I am not a Java programmer so there may be easier ways of doing 
things (I hope so). Also, I have not yet purged unnecessary imports. My own 
code is a bit more complicated as whether I use "name" or some other 
property to create the slug varies by class (my code has a switch in there).

Hope this helps.

```java
package mysite.hooks;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantLock;
import com.orientechnologies.orient.core.hook.ODocumentHookAbstract;
import com.orientechnologies.orient.core.hook.ORecordHook;
import com.orientechnologies.orient.core.hook.ORecordHookAbstract;
import com.orientechnologies.orient.core.db.ODatabaseLifecycleListener;
import com.orientechnologies.orient.core.db.ODatabase;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;

public class setSlug extends ODocumentHookAbstract implements ORecordHook {

  private static Properties diacritics;

  public setSlug() {
    setIncludeClasses(...);

    diacritics = new Properties();

    diacritics.put("\u0041", "A");
    diacritics.put("\u24B6", "A");
    diacritics.put("\uFF21", "A");
    // remaining diacritics
  }

  @Override
  public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
    return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
  }

  public String convertChar( String s ) {
    try {
      return diacritics.get(s).toString();
    } catch (Exception e) {
      return "";
    }
  }

  public RESULT onRecordBeforeCreate( ODocument iDocument ) {
    String temp = "";
    String slug;
    int    flag;

    String text = iDocument.field("name", OType.STRING);

    for(char c : text.toCharArray()) {
      temp += convertChar(Character.toString(c));
    }

    slug = temp.replaceAll("[_]+", "-").toLowerCase();

    try {
      OSQLSynchQuery<ODocument> query =
          new OSQLSynchQuery<ODocument>("select max(flag) from " +
            iDocument.getClassName() + " where slug = ?");
      List<ODocument> result =
        iDocument.getDatabase().command(query).execute(slug);

      flag =
        ((Integer) result.get(0).field("max", OType.INTEGER)).intValue() + 
1;

      iDocument.field("flag", flag);
    } catch (Exception e) {
      iDocument.field("flag", 0);
    }

    iDocument.field("slug", slug);

    return ORecordHook.RESULT.RECORD_CHANGED;
  }
}
```

On Saturday, August 22, 2015 at 4:00:01 AM UTC+12, Miguel wrote:
>
> Hi Charles,
>
> Could you please provide some example code? I'm looking at doing something 
> similar and I cannot find any documentation on how to code a hook in 
> javascript.
> Your help would be much appreciated.
>
> Miguel
>
> Le mardi 11 août 2015 06:02:22 UTC-4, Charles Munat a écrit :
>>
>> I did that and managed to get it working. A slug is used in a URL to 
>> identify a resource, usually by "traincasing" its name, e.g., 
>> /cool-blog-post-2015-07-31. It makes for a better, more aesthetic, and 
>> human-readable URL compared to using UUIDs.
>>
>> Chas.
>>
>> On Tuesday, August 11, 2015 at 5:59:07 AM UTC+12, David Carr wrote:
>>>
>>> I'm not familiar with the term slug but you should be able to do this 
>>> via a hook (aka insert trigger). I have a few insert triggers that work 
>>> well. Try to follow the tutorial here 
>>> http://orientdb.com/docs/last/Tutorial-Java-Hooks.html.
>>>
>>> On Sunday, August 9, 2015 at 3:27:13 AM UTC-6, Charles Munat wrote:
>>>>
>>>> I want to create a slug from a user's name and save it when the record 
>>>> is created. In PostgreSQL this is easy with a procedure and a trigger, but 
>>>> I can't for the life of me figure out how to do it in OrientDB. I need to 
>>>> a) get hold of the name field of the document within the javascript 
>>>> function onBeforeCreate, and b) I need to compare the slug to all the 
>>>> other 
>>>> slugs to ensure that it is unqiue, and to add an incremented integer if it 
>>>> is not.
>>>>
>>>> It would be nice to have a simple function that I could call on default 
>>>> for the slug field (better still if I could pass it the name of the field 
>>>> or fields to be used to create the slug). Barring that, a dynamic hook.
>>>>
>>>> What is the best practice, or can someone provide a link to where this 
>>>> is covered in the documentation? I can't find it.
>>>>
>>>> TIA,
>>>>
>>>> Chas. Munat
>>>>
>>>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to