/*
 *   Blumoon Technologies Corp.
 *   Copyright 2013.
 */
package com.blumoon.springmvcredis.service;

import com.blumoon.springmvcredis.keys.SetKeys;
import com.blumoon.springmvcredis.keys.StringKeys;
import com.blumoon.springmvcredis.util.KeysUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 *
 * @author Gene Diaz Jr. <gene@blumoon.mobi>
 */
@Service
public class SampleService {

    private static final Log LOG = LogFactory.getLog(SampleService.class);
    @Autowired
    private StringRedisTemplate template;

    public Object sample(String sample) {

        KeysUtil keysUtil = new KeysUtil();

        // generate id
        String sampleId = keysUtil.generateUUID();

        // check from set if id exists
        if (template.opsForSet().isMember(SetKeys.SAMPLE, sampleId)) {
            // regenerate id
            sampleId = keysUtil.generateUUID();
        }

        // add id to set and check if added
        if (1 == template.opsForSet().add(SetKeys.SAMPLE, sampleId)) {

            // set next object if id was added
            boolean result = template.opsForValue().setIfAbsent(StringKeys.SAMPLE.concat(KeysUtil.DELIMITER).concat(sampleId), sample);

            LOG.debug(new StringBuilder("SampleId: ")
                    .append(sample)
                    .append(result));
        }

        return sampleId;
    }

}

@@ -1322,16 +1322,31 @@
 fAbsent(
+keysUtil.build(
 StringKe
@@ -1358,43 +1358,10 @@
 MPLE
-.concat(KeysUtil.DELIMITER).concat(
+, 
 samp
@@ -1465,16 +1465,93 @@
 d(sample
+Id).append(%22 Value: %22)%0A                    .append(sample).append(%22 Result: %22
 )%0A      

update sampleservice to use key.build