# 🔗 Document Association Chain Tracker

## 📊 **Discovered Chain (So Far)**

```
Position → Document → Shows Content Of
─────────────────────────────────────────
   1     → PL11089  → PL689      ✅ Verified
   2     → PL689    → BP102      ✅ Verified
   3     → BP102    → PL6204     ✅ Verified
   4     → PL6204   → PL12321    ✅ Verified
   5     → PL12321  → ???????    ⏳ Checking now...
```

**Total Documents in Chain**: At least **5** (possibly more)

---

## 🎯 **Possible Outcomes**

### **Outcome A: 5-Way Circle (Best Case)**
```
PL12321 → PL11089 (completes circle)

Chain: PL11089 → PL689 → BP102 → PL6204 → PL12321 → [PL11089]
```
**Fix**: 5-document circular redirect workaround  
**Timeline**: Can implement today ✅  
**Complexity**: Medium

---

### **Outcome B: PL12321 is Correct**
```
PL12321 → PL12321 (correct association)

Chain: PL11089 → PL689 → BP102 → PL6204 → PL12321 ✅
```
**Fix**: 4-document redirect workaround (PL12321 excluded)  
**Timeline**: Can implement today ✅  
**Complexity**: Medium

---

### **Outcome C: Chain Extends (More Documents)**
```
PL12321 → [Another Document] (e.g., PL999, BP500, etc.)

Chain continues: PL11089 → ... → PL12321 → ??? → ??? → ...
```
**Fix**: Need to trace full chain first  
**Timeline**: 1-2 days (depending on length)  
**Complexity**: High - systematic issue

---

## 🔍 **Pattern Analysis**

### **Common Characteristics:**
- All documents from **2015** scanning
- Perfect **sequential chain** (not random)
- Affects **all document types** (History Card, Property File, Land Form)
- No exceptions found so far

### **Root Cause Hypothesis:**
During 2015 data entry:
1. Multiple documents scanned in batch
2. Files saved correctly (File1, File2, File3, File4, File5...)
3. Document numbers listed (PL11089, PL689, BP102, PL6204, PL12321...)
4. **Labels applied in wrong order** (rotated by 1 position)
5. Result: Each file got the next document's label

### **Evidence:**
- ✅ Perfect sequential pattern
- ✅ All from same time period
- ✅ No random errors
- ✅ Affects all document types consistently

---

## 📋 **Investigation Checklist**

- [x] Verify PL11089 → Shows PL689
- [x] Verify PL689 → Shows BP102
- [x] Verify BP102 → Shows PL6204
- [x] Verify PL6204 → Shows PL12321
- [ ] **Verify PL12321 → Shows ???** ← CURRENT STEP
- [ ] Continue chain if needed
- [ ] Identify full scope
- [ ] Implement fix

---

## 🚀 **Next Steps**

### **Immediate (Now):**
```bash
cd /home/plagis/workspace/plagis_aumentum
./verify_pl12321.sh
```

### **After PL12321 Verification:**

#### If PL12321 shows PL11089:
1. ✅ Circle confirmed - 5 documents total
2. Implement 5-way redirect workaround
3. Test all 5 documents
4. Deploy fix

#### If PL12321 shows PL12321:
1. ✅ Chain ends - 4 documents affected
2. Implement 4-document workaround
3. Test all 4 documents
4. Deploy fix

#### If PL12321 shows [Other]:
1. ❌ Chain continues
2. Create script to verify next document
3. Continue tracing until:
   - Circle completes, OR
   - We find a correct document, OR
   - We reach max reasonable length (10+ suggests database-wide issue)

---

## 💡 **Database Correction Strategy**

If chain exceeds 5-10 documents, workaround becomes impractical.

**Alternative Approach:**
1. Export complete association mapping
2. SQL UPDATE script to fix all at once
3. Backup database first
4. Apply corrections
5. Verify all documents

**SQL Pattern:**
```sql
-- For each document in chain
UPDATE LRSAdmin.alf_node_properties
SET string_value = 'CORRECT_DOC_NUMBER'
WHERE node_id = WRONG_NODE_ID
AND qname_id IN (SELECT id FROM LRSAdmin.alf_qname WHERE local_name IN ('targetRids','sourceRids'));
```

---

## 📊 **Scope Estimation**

### **If 5-10 documents:**
→ Python workaround feasible  
→ Can implement in 1 day  
→ No database changes needed

### **If 10-20 documents:**
→ Hybrid approach recommended  
→ Workaround for critical docs  
→ Plan database correction  
→ Timeline: 2-3 days

### **If 20+ documents:**
→ Database correction mandatory  
→ Full audit of 2015 documents  
→ Systematic fix required  
→ Timeline: 1 week

---

## 📞 **Current Status**

**Position in Chain**: Document #5 (PL12321)  
**Action Required**: Check what PL12321 actually shows  
**Command**: `./verify_pl12321.sh`

**Report Format**:
```
PL12321 shows: _____________
```

---

**Last Updated**: Now  
**Status**: ⏳ Awaiting PL12321 verification

